Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a column in the datagrid based on a condition 1

Status
Not open for further replies.

ravula2000

IS-IT--Management
Mar 8, 2002
205
US
In my datagrid I need to display the buttoncolumn based on a condition..
I can't write If condition in the grid like
<columns>
<%If x=10 then%>
<asp:Buttoncolumn Text=&quot;Edit&quot; HeaderText=&quot;*&quot; />
<%End If%>
</columns>

I dont know whether it can be possible using OnItemBound event.
Please help me..

Thanks
Srini



 
Definately - use the OnItemDataBound event. In that event handler you can get a reference to the DataItem currently being bound. From there you can conditionally format your grid item based on the DataItem's properties.

something like this: (sorry I don't know VB.net ;))
Code:
//event handler
private void grid_OnItemDataBound(object sender, DataGridItemEventArgs e)
{
    //get a reference to the data item (cast as a datarowview)
    DataRowView row = (DataRowView)e.Item.DataItem;

    //check the value of the coumn you want and conditioanlly apply formatting
    if(row[&quot;variableColumn&quot;] == &quot;a condition&quot;)
    {
        //make your changes...

    }
}

Good Luck,

David
[pipe]
 
Would it be easier to just make the button column visible property true or false depending on the condition you are measuring?

If x=10 then
DataGrid.Columns(0).visible=&quot;false&quot;
else
DataGrid.Columns(0).visible=&quot;false&quot;
end if

If you know your value when a postback is made, then just put this in a sub that is called from the Page_Load sub.

TT
 
I've had to hide columns in the past (mainly button columns like yourself), and always used the suggestion that Tom made.

No reason to hide it row by row, just get the whole column out of there in one action.

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top