DropDownList ddl = (DropDownList) e.Item.FindControl("DropDownList1"); ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(currentgenre)); } } Selecting Multiple Items Using a Check Box (Hotmail Model) In applications such as Microsoft Hotmail®, users can "select" rows by checking a box and then performing an operation on all the selected rows — for example, delete them or copy them.
To add functionality like this, add a template column to the grid and put a check box into the column. When the page runs, users will be able to check the items they want to work with.
To actually perform the user action, you can walk the grid's Items collection, looking into the appropriate column (cell) to see if the check box is checked. The following example shows how you can delete rows in a dataset corresponding to the items that a user has checked. The dataset, called dsBooks1, is assumed to contain a table called Books.
' Visual Basic Private Sub btnDelete_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDelete.Click ' Walk the grid looking for selected rows Dim i As Integer = 0 Dim cb As CheckBox Dim dgi As DataGridItem Dim bookid As Integer Dim dr As dsBooks.BooksRow For Each dgi In DataGrid1.Items cb = CType(dgi.Cells(0).Controls(1), CheckBox) If cb.Checked Then ' Determine the key of the selected record ... bookid = CType(DataGrid1.DataKeys(i), Integer) ' ... get a pointer to the corresponding dataset record ... dr = Me.DsBooks1.Books.FindBybookid(bookid) ' ... and delete it. dr.Delete() End If i += 1 Next Me.SqlDataAdapter1.Update(DsBooks1) Me.SqlDataAdapter1.Fill(DsBooks1) DataGrid1.DataBind() End Sub
// C# private void btnDelete_Click(object sender, System.EventArgs e) { int i = 0; CheckBox cb; int bookid; dsBooks.BooksRow dr; foreach(DataGridItem dgi in this.DataGrid1.Items) { cb = (CheckBox) dgi.Cells[0].Controls[1]; if(cb.Checked) { // Determine the key of the selected record ... bookid = (int) DataGrid1.DataKeys[i]; // ... get a pointer to the corresponding dataset record ... dr = this.dsBooks1.Books.FindBybookid(bookid); // ... and delete it. dr.Delete(); } i++; } this.sqlDataAdapter1.Update(this.dsBooks1); this.sqlDataAdapter1.Fill(this.dsBooks1); DataGrid1.DataBind();