Solved

Template columns in datagrid

Posted on 2007-11-30
12
1,752 Views
Last Modified: 2013-12-16
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.
0
Comment
Question by:VD1234
12 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 20384116
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
0
 

Author Comment

by:VD1234
ID: 20384319
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
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20384361
can you post some code?
Every time a grid is bound, the ItemDatabound event **is** triggered. period.
0
 

Author Comment

by:VD1234
ID: 20384426
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.
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20384490
from what I can tell, it looks ok....
are you sure the new data is getting added to dtDynamic?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:VD1234
ID: 20384558
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.

0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 20384615
ok...was just making sure.

based on the column you have:
<asp:TemplateColumn HeaderText="Visible">
<ItemTemplate>
<asp:CheckBox ID="chkVisible" Runat="server" Width="40px" />                  
</ItemTemplate>
</asp:TemplateColumn>


i don't see where it's supposed to get the text when you do this:
chkVisibleField.Checked = Convert.ToBoolean(e.Item.Cells[8].Text);

are you trying to set the chkbox based on the column before it at this line:
<asp:BoundColumn DataField="IsVisible" HeaderText="IsVisible" visible="false"></asp:BoundColumn>
then try:
chkVisibleField.Checked = Convert.ToBoolean(e.Item.Cells[7].Text);

...but i'm not sure if that would work since you set the column to visible=false

have you tried using the dataitem?
chkVisibleField.Checked = Convert.ToBoolean(e.Item.DataItem("IsVisible"));
0
 
LVL 10

Expert Comment

by:Alpesh Patel
ID: 20384906

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
0
 

Author Comment

by:VD1234
ID: 20385015
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
0
 

Author Comment

by:VD1234
ID: 20385076
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.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20591178
Forced accept.

Computer101
EE Admin
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now