Reputation: 11916
I have three fields being displayed in the gridview. Depending on the value of first field, I have to either display or hide the second two fields.
The following code is what I've tried so far but I don't know how to get the full solution.
Could anybody have a look please?
The three fields are
1) activeStatus
2)DateMadeInactive
3)Comments
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool activeStatus=Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem,"Active"));
if(activeStatus)
{
// I need to display the activeStatus columns
}
else
{
// I need to hide activeStatus Column and Display the DatemadeInactive and Comments
}
}
}
Upvotes: 0
Views: 87
Reputation:
I did something like this using, IIRC, the CellFormatting event. It gives you an opportunity to test the value of each cell and replace it or other cells in the row based on that. In my case I was replacing numeric values with looked-up strings and altering background color.
Upvotes: 0
Reputation: 1710
What I gather from your question is you want to know how to hide or display any given column in a DataGridView. If so, you just need to add the column to the DataGridView, either with a DataSource or manually, and then hide any columns you don't want by doing the following:
dataGridView1.Columns["YourColumnName"].Visible = false;
Upvotes: 1
Reputation: 172200
You can put conditionals into template fields:
<asp:GridView ... runat="server">
<Columns>
... your other fields ...
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label Text='<%# (bool)Eval("Active")
? Eval("activeStatus")
: Eval("DateMadeInactive", "Inactive since {0}") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Upvotes: 1