0

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.

suryakiran
  • 1,976
  • 25
  • 41

3 Answers3

0

You need to write your own CustomeField/Custom GridView Column class. If you search with "GridView Custom field" over net, you will get many examples. AutoGenerateColumns works with it. One need to write lot code in codebehind :)

Saar
  • 8,286
  • 5
  • 30
  • 32
  • looks to be a very lengthy process. Will there be any any to finding and replacing the checkbox control on the Row databound or any other grid view events? – suryakiran Aug 09 '11 at 06:42
0

You can use template field with auto generate columns.

Savas
  • 77
  • 2
  • 10
0

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.

Alexander
  • 23,432
  • 11
  • 63
  • 73
suryakiran
  • 1,976
  • 25
  • 41