Solved

Template columns in datagrid

Posted on 2007-11-30
12
1,771 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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
 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

623 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