Link to home
Start Free TrialLog in
Avatar of VD1234
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.
Avatar of samtran0331
samtran0331
Flag of United States of America image

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
Avatar of VD1234
VD1234

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
can you post some code?
Every time a grid is bound, the ItemDatabound event **is** triggered. period.
Avatar of VD1234

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:BoundColumn>
<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_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if (((e.Item.ItemType == ListItemType.Item)
                       || (e.Item.ItemType == ListItemType.AlternatingItem)))
            {

                CheckBox chkVisibleField = e.Item.Cells[8].FindControl("chkVisible") as CheckBox;
                chkVisibleField.Checked = Convert.ToBoolean(e.Item.Cells[8].Text);
               
            }
        }

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?
Avatar of VD1234

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
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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
Avatar of VD1234

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.Cells[7].Text);
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
Avatar of VD1234

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.ItemDataBound += new DataGridItemEventHandler(dgDynamicFields_ItemDataBound);
            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