I have a Grid view for which columns are auto-generated. The binding data-table would be having a Bit(Boolean) column. Now when the data is binded, the check-box field is generated in place of the Bit Column.
Requirement
Check box fields needs to be replaced with Radio button list, with two options as Approved and Rejected based on the bit column mentioned above.
Constraint
I cannot set the auto-generated columns as false, as the number of columns in the grid view will vary based the filter selected. But every time it have the bit column.
After a long research on Google. I got the solution, but it doesn't look to be a convincing solution.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
int Cellix = -1;
Cellix = GetBooleanCellIndex(e.Row);
if (Cellix != -1)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButtonList rbnl = new RadioButtonList();
rbnl.ID = "rbn_Status";
rbnl.RepeatDirection = RepeatDirection.Horizontal;
rbnl.Items.Add(new ListItem("Open", "0"));
rbnl.Items.Add(new ListItem("Close", "1"));
rbnl.SelectedValue = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "status")).ToString();
e.Row.Cells[Cellix].Controls.Clear();
e.Row.Cells[Cellix].Controls.Add(rbnl);
}
}
}
private int GetBooleanCellIndex(GridViewRow gvrow)
{
int CellIndex = 0;
Boolean dummy = true;
foreach (DataControlFieldCell cell in gvrow.Cells)
{
AutoGeneratedField At = null;
if (cell.ContainingField.GetType().Name == "AutoGeneratedField")
{
At = (AutoGeneratedField)cell.ContainingField;
if (At.DataType.Name == dummy.GetType().Name)
return CellIndex;
CellIndex++;
}
}
return -1;
}
So I am expecting a further refinements from you guys.