Solved

Template columns in datagrid

Posted on 2007-11-30
12
1,748 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
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.

708 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

17 Experts available now in Live!

Get 1:1 Help Now