DataBind a GridView Immediately?
Hi all,
I'm loading data into a DataTable and then binding it to my grid view. The thing is I want to then dynamically add a CheckBoxColumn which I'm doing by creating my own template class. That's all fine except for 2 things...
1. I want to add the new CheckBoxColumn to the end column of the grid.
2. I want to then scan the results of the grid and depending on the results, enable or disable each checkbox on the row.
The problem is that it doesn't actually DataBind() until the page is rendered. That means that I can't scan the results or add to the end using GridView.Insert(GridView.Columns.Count-1, CheckBoxColumn) as there are no results in the Grid. Does anyone know a way around this pesky problem. I've already wasted my Saturday trying to find out!
Thanks all.
Rob
Puran KaushalPosted Jul 7, 2008, 8:26 AM
its is not difficult
you can fill the datatable
in any of the following even of grids
protected
void GridVeiw1_PreRender(object sender, EventArgs e){
fill datatable here
}
protected
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
//You can also fill datatable here and implement you logic as your requirments
}
Guest UserPosted Jul 1, 2008, 1:39 PM
1) Call DataBind() on the gridview manually in the code-behind.
2) Instead of adding a column to the grid dynamically, add a boolean column (datatype "bit" in SQL Server) to your query's resultset and set its values there, as in:
SELECT
YourFirstField,
...,
CheckBoxColumn = CAST (CASE WHEN (YourConditionIsTrue) THEN 1 ELSE 0 END AS BIT)
You can then add a static checkbox column to your grid and it can use the value of CheckBoxColumn for the Checked property.