Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Template columns in datagrid

Posted on 2007-11-30
12
Medium Priority
?
1,784 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
11 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
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.
Suggested Courses

877 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