VD1234
asked on
Template columns in datagrid
I have a datagrid, which has a template column (checkbox). I assign the value to each row in the ItemDatabound event. This works great when page is redered. However when I add a row to this datagrid, how do I assign values to the template columns?
Any help will be greatly appreciated. Thanks in adavance.
Any help will be greatly appreciated. Thanks in adavance.
Not quite sure I understand, but after you add a row, if you bind the grid again, the new row would show up and your ItemDatabound would catch it just like it would an "old" row
ASKER
On the add button column, I add this row to the datatable and bind the datagrid. However the ItemDatabound event was not trigerred.
Than ks
Than ks
can you post some code?
Every time a grid is bound, the ItemDatabound event **is** triggered. period.
Every time a grid is bound, the ItemDatabound event **is** triggered. period.
ASKER
Some of the Design:
<asp:DataGrid id="dgDynamicFields" Runat="server" Width="600px" DataKeyField="FieldId"
CssClass="griddata" AutoGenerateColumns="False ">
<asp:BoundColumn DataField="IsVisible" HeaderText="IsVisible" visible="false"></asp:Boun dColumn>
<asp:TemplateColumn HeaderText="Visible">
<ItemTemplate>
<asp:CheckBox ID="chkVisible" Runat="server" Width="40px" />
</ItemTemplate>
</asp:TemplateColumn>
On addrow button click:
//first add the row to the datatable then I do the following:
DataView dvDynamicFields = new DataView(dtDynamic);
dgDynamicFields.DataSource = dvDynamicFields;
dgDynamicFields.DataBind() ;
The following is the itemdatabound event:
private void dgDynamicFields_ItemDataBo und(object sender, System.Web.UI.WebControls. DataGridIt emEventArg s e)
{
if (((e.Item.ItemType == ListItemType.Item)
|| (e.Item.ItemType == ListItemType.AlternatingIt em)))
{
CheckBox chkVisibleField = e.Item.Cells[8].FindContro l("chkVisi ble") as CheckBox;
chkVisibleField.Checked = Convert.ToBoolean(e.Item.C ells[8].Te xt);
}
}
Please let me know if you need me to post any other sections. Anyt help will greatly be appreciated.Thanks a lot.
<asp:DataGrid id="dgDynamicFields" Runat="server" Width="600px" DataKeyField="FieldId"
CssClass="griddata" AutoGenerateColumns="False
<asp:BoundColumn DataField="IsVisible" HeaderText="IsVisible" visible="false"></asp:Boun
<asp:TemplateColumn HeaderText="Visible">
<ItemTemplate>
<asp:CheckBox ID="chkVisible" Runat="server" Width="40px" />
</ItemTemplate>
</asp:TemplateColumn>
On addrow button click:
//first add the row to the datatable then I do the following:
DataView dvDynamicFields = new DataView(dtDynamic);
dgDynamicFields.DataSource
dgDynamicFields.DataBind()
The following is the itemdatabound event:
private void dgDynamicFields_ItemDataBo
{
if (((e.Item.ItemType == ListItemType.Item)
|| (e.Item.ItemType == ListItemType.AlternatingIt
{
CheckBox chkVisibleField = e.Item.Cells[8].FindContro
chkVisibleField.Checked = Convert.ToBoolean(e.Item.C
}
}
Please let me know if you need me to post any other sections. Anyt help will greatly be appreciated.Thanks a lot.
from what I can tell, it looks ok....
are you sure the new data is getting added to dtDynamic?
are you sure the new data is getting added to dtDynamic?
ASKER
yes. the data is added to the dtdynamic becuase I have other bound columns that show up. all the rows, right from the row 1 after the addrow button click loose the values that were assigned in the itemdatabound event.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
first of all DataGrid doesnt have any facility directly to add dynamic row
so its depends how u ADD new row in DataGrid
jus clarify me i can give you solutions. as i had done lots of time to add dynamically data with DataGrid and GridView
thanx
ASKER
YEs, I am trying to assign the value form the previous bound column. Sorry it was a typo as you said my code does read the following in my application:
chkVisibleField.Checked = Convert.ToBoolean(e.Item.C ells[7].Te xt);
However all this work perfectly during the initial page rendering. This column and a couple of other columns I assign value during the ITemdatabound event get assigned correct values. However after I add row, these template columns are not assigned the correct values. When I put a break point in the Item Databound event control never goes to this section after the Addrow button click although I call Datagrid.databind().
Thanks
chkVisibleField.Checked = Convert.ToBoolean(e.Item.C
However all this work perfectly during the initial page rendering. This column and a couple of other columns I assign value during the ITemdatabound event get assigned correct values. However after I add row, these template columns are not assigned the correct values. When I put a break point in the Item Databound event control never goes to this section after the Addrow button click although I call Datagrid.databind().
Thanks
ASKER
Samtran:
Ok the issue was that I had wired the Item Databound event inside the if(!Ispostback) section. Now I moved it out and the event is trigerred and the reqauired values are assigned ot all the columns;
dgDynamicFields.ItemDataBo und += new DataGridItemEventHandler(d gDynamicFi elds_ItemD ataBound);
if (!IsPostBack)
{
}
However, you had spent a lot of time trying to help me and giving me suggestons. Probaly if I had included this code you would have spot the issue. So I am going to go ahead and assign the points to you.
Thank you for your help and time.
Ok the issue was that I had wired the Item Databound event inside the if(!Ispostback) section. Now I moved it out and the event is trigerred and the reqauired values are assigned ot all the columns;
dgDynamicFields.ItemDataBo
if (!IsPostBack)
{
}
However, you had spent a lot of time trying to help me and giving me suggestons. Probaly if I had included this code you would have spot the issue. So I am going to go ahead and assign the points to you.
Thank you for your help and time.
Forced accept.
Computer101
EE Admin
Computer101
EE Admin