Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView in ASP.NET

Status
Not open for further replies.

AKtuality

IS-IT--Management
Jun 2, 2011
3
CA
Hi

I am quite new to the ASP.NET framework and I am trying to design a datagridview table that does the following:

1. Retrive data from an existing database using a connection string
2. Displays that data
3. Allows user to edit and make changes to every row in the datagridview.
4. Allows to user to insert data (at the footer) into the dataset through the datagridview table using textboxes and dropdownlists
5. The dropdownlist contains elements from another database retrieved through another connection string.

lets say for example that my dataset contained the following columns:
1. ID Number
2. First Name
3. Last Name

Thank You in advance
 
Also keep in mind that the dropdownlist is a part of the datagridview

Thanks
 
Are you asking us to write the code for you? What have you done so far? Why not start, and post questions as you run into problems?
 
fair enough..

lets start with the dropdownlist..
i have the following code..

private void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow dataGridView = (GridViewRow)(((Control)sender).NamingContainer);

DropDownList ddl = (DropDownList)dataGridView.FindControl("ddl");

if (!IsPostBack)
{
string connString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();

DataSet dataSet = new DataSet();
string sql = "SELECT Employee_Number FROM Employee_Master ORDER BY Employee_Number";
SqlCommand cmd = new SqlCommand(sql, conn);

SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);

ddl.DataSource = dataSet;
ddl.DataValueField = "Employee_Number";
ddl.DataTextField = "Employee_Number";
ddl.DataBind();

ddl.Items.Insert(0, "<---Select--->");
}
}

ddl is the dropdownlist
cn is the connection string

The dropdownlist is not being populated.. what do i do?!
 
I am not sure when in the SelectedIndexChanged event of the ddl, you are trying to find it.

I am glad to see you are not using the datasource controls. Don't, write the code yourself as you are, but I encourage you using stored procedures instead. As you learn more, you can look into an ORM such as Microsoft's Entity Framework.

If your ddl is in each row of your grid then what you need to do is to use the RowDataBound event of Gridview. In there, first check if you are on a datarow, if so, then use FindControl() to find the ddl, then bind the ddl as needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top