By Creating the checkboxes dynamically in datagrid by using Template Column,checkboxes with same contents are repeating in all rows.So i want to restrict the checkboxes(one or more) in cells(0),cells(1),cells(2) etc...So instead of using itemtemplte,we can create the checkbox dynamically by the following way....Enjoy the coding.....Cheers
BindGrid() { Declare an sqlconnection and connect it to the database and fill it in dataset by using sqldataadapter DataTable dttable = new DataTable();
for (int i = 0; i < ds.Tables["main_type"].Rows.Count; i++) { for (int k = 0; k < ds.Tables["sub_type"].Rows.Count; k++) { if (Convert.ToInt32(ds.Tables["sub_type"].Rows[k]["type_id"]) == Convert.ToInt32(ds.Tables["main_type"].Rows[i]["type_id"])) { if (!dttable.Columns.Contains(ds.Tables["main_type"].Rows[i]["tx_type"].ToString())) { dttable.Columns.Add(new DataColumn(ds.Tables["main_type"].Rows[i]["tx_type"].ToString())); } } } } DataRow drNewRow = dttable.NewRow(); dttable.Rows.Add(drNewRow); ///bind it to the datagrid
}
public int total_count = 0; public int cell_count = 1; protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) {
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) { if (total_count == 0) { for (int i = 0; i < ds.Tables["main_type"].Rows.Count; i++) { total_count = i; if (ds.Tables["main_type"].Rows[i]["type_id"] != null) { for (int k = 0; k < ds.Tables["sub_type"].Rows.Count; k++) { if (Convert.ToInt32(ds.Tables["sub_type"].Rows[k].ItemArray[0]) == Convert.ToInt32(ds.Tables["main_type"].Rows[i]["type_id"])) { CheckBox valCheck = new CheckBox(); ///For identifying which checkbox is clicked ///you can use valcheck.ID = .... valCheck.Text = ds.Tables["sub_code"].Rows[k].ItemArray[1].ToString() + " "; e.Item.Cells[cell_count].Controls.Add(valCheck); } } cell_count++; } } } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|