//check that each textbox contains only numbers //... }
The problem with the code above is that the grid.Rows.Count equals 0. I can see the data so I know that can't be correct. Am I using the right property (Rows) to iterate through the grid?
I gave it a try, it seems to work for me. if grid refers to your Gridview instance, and if it has been filled with data ( through an objectDatasource or by any other means) , it should work. do you have more snippet of the code behind?
Still in search of the far side.
--------------------------------
A code snippet may help you, but a reference to the doc will teach you something for later...Don't just try to solve somebody's problem with a snippet, teach him how to solve it.
Could you let me know which event handler have you put your code in? You can see the data when it is loaded at Page_Load event. But when you click a button to post it back, the data might be lost since the whole page is reloaded.
You have to make sure that DataBind() method is called before you check for row count.
Sincerely,
Kevin Yu
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Mark as Not Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Yes it is a button click event that triggers the code. When I do check even the GridView columns count it is 0 so I'm losing the data. The confusing part though is that this code worked in asp.net 1.1. The only difference is that I'm using AJAX (http://ajax.asp.net/).
I can't call DataBind() because I have text boxes that are edited and I want to check the user's input first (ensure only numbers entered) and then save the items to a DataTable as shown below:
I'm not using the ObjectDataSource or SQLDataSource because I'm inheriting this project and I'm thinking it would be a bigger project to change the code rather than just modifying the existing code.
Please check if ViewState has been enabled. By default all data is cached in ViewState, and data will be loaded before entering the button.click event handler.
Sincerely,
Kevin Yu
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Mark as Not Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Please check if ViewState has been enabled. By default all data is cached in ViewState, and data will be loaded before entering the button.click event handler.
Utter nonsense. Not all data is cached in ViewState by default or otherwise.
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
Okay figured out the problem (unfortunately not the solution). I'm using
dynamic template in my GridView. (I know I should have mentioned that in my previous posts [;)]There seems to be a problem in that it doesn't post back the items in the grid. I don't know why.
If worse comes to worse the Items are in the Request.Form.AllKeys and I could parse that out but I was hoping to find a cleaner solution.
I assume that the item templates were not re-created after post back. Is this what you mean in your last post?
I check the code from CodeProject, it calls loadDynamicGridWithTemplateColumn() each time page loads. Because the page doesn't contains template information, since it is created at runtime.
Are you calling your template creation method every time page loads? If you have problems checking this, please post your Page_Load() event handler. Thanks!
Sincerely,
Kevin Yu
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Mark as Not Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Bill_N
Member
38 Points
23 Posts
datagrid.Items.Count to GridView.Rows.Count
Jun 22, 2007 03:24 PM|LINK
I'm having trouble with a particular page where I have replaced the DataGrid with a GridView. I have the following 1.1 code that worked nicely:
I'm trying to do the equivalent with this code:
zaladane
Participant
850 Points
194 Posts
Re: datagrid.Items.Count to GridView.Rows.Count
Jun 22, 2007 05:37 PM|LINK
I gave it a try, it seems to work for me. if grid refers to your Gridview instance, and if it has been filled with data ( through an objectDatasource or by any other means) , it should work. do you have more snippet of the code behind?
--------------------------------
A code snippet may help you, but a reference to the doc will teach you something for later...Don't just try to solve somebody's problem with a snippet, teach him how to solve it.
Kevin Yu - M...
All-Star
19021 Points
1467 Posts
Re: datagrid.Items.Count to GridView.Rows.Count
Jun 25, 2007 07:25 AM|LINK
Hi Bill,
Could you let me know which event handler have you put your code in? You can see the data when it is loaded at Page_Load event. But when you click a button to post it back, the data might be lost since the whole page is reloaded.
You have to make sure that DataBind() method is called before you check for row count.
Kevin Yu
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Mark as Not Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Bill_N
Member
38 Points
23 Posts
Re: datagrid.Items.Count to GridView.Rows.Count
Jun 25, 2007 02:31 PM|LINK
Thanks for the reply.
Yes it is a button click event that triggers the code. When I do check even the GridView columns count it is 0 so I'm losing the data. The confusing part though is that this code worked in asp.net 1.1. The only difference is that I'm using AJAX (http://ajax.asp.net/).
I can't call DataBind() because I have text boxes that are edited and I want to check the user's input first (ensure only numbers entered) and then save the items to a DataTable as shown below:
public DataTable getUpdatedDatagrid(GridView grid,DataTable dt1,int userId) { try { //Loop through GridView Items foreach (GridViewRow grdRow in grid.Rows) { TextBox txt_Day = (TextBox)(grdRow.FindControl("txtDay")); TextBox txt_PremiseID = (TextBox)(grdRow.FindControl("txtPremiseID")); TextBox txt_M1 = (TextBox)(grdRow.FindControl("txtM1")); DataRow dr = dt1.NewRow(); dr[0] = userId; dr[1] = Convert.ToInt32(txt_Day.Text.Trim()); dr[2] = Convert.ToDecimal(txt_PremiseID.Text.Trim()); dr[3] = Convert.ToDecimal(txt_M1.Text.Trim()); dt1.Rows.Add(dr); } } catch (Exception ex) { Session["CurrentError"] = ex.Message; Server.Transfer("Errorpage.aspx"); } return dt1; }I'm not using the ObjectDataSource or SQLDataSource because I'm inheriting this project and I'm thinking it would be a bigger project to change the code rather than just modifying the existing code.
Any help would be greatly appreciated.
Thanks, Bill N
Kevin Yu - M...
All-Star
19021 Points
1467 Posts
Re: datagrid.Items.Count to GridView.Rows.Count
Jun 27, 2007 07:40 AM|LINK
Hi,
Please check if ViewState has been enabled. By default all data is cached in ViewState, and data will be loaded before entering the button.click event handler.
Kevin Yu
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Mark as Not Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
naturehermit
Star
14610 Points
3046 Posts
Re: datagrid.Items.Count to GridView.Rows.Count
Jun 27, 2007 09:11 AM|LINK
Utter nonsense. Not all data is cached in ViewState by default or otherwise.
Bill_N
Member
38 Points
23 Posts
Re: datagrid.Items.Count to GridView.Rows.Count
Jun 27, 2007 03:21 PM|LINK
Okay figured out the problem (unfortunately not the solution). I'm using dynamic template in my GridView. (I know I should have mentioned that in my previous posts [;)]There seems to be a problem in that it doesn't post back the items in the grid. I don't know why.
If worse comes to worse the Items are in the Request.Form.AllKeys and I could parse that out but I was hoping to find a cleaner solution.
Any help would be appreciated, Bill N
Kevin Yu - M...
All-Star
19021 Points
1467 Posts
Re: datagrid.Items.Count to GridView.Rows.Count
Jun 28, 2007 07:18 AM|LINK
Hi Bill,
I assume that the item templates were not re-created after post back. Is this what you mean in your last post?
I check the code from CodeProject, it calls loadDynamicGridWithTemplateColumn() each time page loads. Because the page doesn't contains template information, since it is created at runtime.
Are you calling your template creation method every time page loads? If you have problems checking this, please post your Page_Load() event handler. Thanks!
Kevin Yu
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Mark as Not Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
naturehermit
Star
14610 Points
3046 Posts
Re: datagrid.Items.Count to GridView.Rows.Count
Jun 28, 2007 08:41 AM|LINK
Could I please see your complete code in order to help you further.