datagrid.Items.Count to GridView.Rows.Counthttp://forums.asp.net/t/1125257.aspx/1?datagrid+Items+Count+to+GridView+Rows+CountThu, 28 Jun 2007 08:41:07 -040011252571768242http://forums.asp.net/p/1125257/1768242.aspx/1?datagrid+Items+Count+to+GridView+Rows+Countdatagrid.Items.Count to GridView.Rows.Count I have migrated an asp.net 1.1 to 2.0 web application. <p>I'm having trouble with a particular page where I have replaced the DataGrid with a GridView. &nbsp; I have the following 1.1 code that worked nicely:</p> <pre class="prettyprint"></pre>&nbsp;<pre class="prettyprint"><span class="kwd">for</span> ( <span class="kwd">int i=0; i &lt;datagrid.Items.Count; i++)</span><datagrid.Items.Count; i++)="" {="" datagriditem="" _item="datagrid.Items[i];" textbox="" txt_day="(TextBox)(_item.FindControl(&lt;span" class="st"><br>{</datagrid.Items.Count;></pre><pre class="prettyprint"> DataGridItem _item = datagrid.Items[i];&nbsp;</pre><pre class="prettyprint"><datagrid.Items.Count; i++)="" {="" datagriditem="" _item="datagrid.Items[i];" textbox="" txt_day="(TextBox)(_item.FindControl(&lt;span" class="st"> TextBox txt_M1=(TextBox)(_item.FindControl(<span class="st">"txtM1"</span>));<br> TextBox txt_M2=(TextBox)(_item.FindControl(<span class="st">"txtM2"</span>));<br> TextBox txt_M3=(TextBox)(_item.FindControl(<span class="st">"txtM3"</span>));<br><br> <span class="cmt">//check to make sure each textbox contains only numbers<br> //...</span> }</datagrid.Items.Count;></pre>&nbsp;<pre class="prettyprint"><datagrid.Items.Count; i++)="" {="" datagriditem="" _item="datagrid.Items[i];" textbox="" txt_day="(TextBox)(_item.FindControl(&lt;span" class="st"></datagrid.Items.Count;></pre><p>I'm trying to do the equivalent with this code:&nbsp; <br></p><pre class="prettyprint"><span class="cmt">//Loop through GridView Items</span><br><span class="kwd">foreach</span> (GridViewRow grdRow <span class="kwd">in</span> grid.Rows)<br>{<br> TextBox txt_Day = (TextBox)(grdRow.FindControl(<span class="st">"txtDay"</span>));<br> TextBox txt_M1 = (TextBox)(grdRow.FindControl(<span class="st">"txtM1"</span>));<br> TextBox txt_M2 = (TextBox)(grdRow.FindControl(<span class="st">"txtM2"</span>));<br> TextBox txt_M3 = (TextBox)(grdRow.FindControl(<span class="st">"txtM3"</span>)); <br><br> <span class="cmt">//check that each textbox contains only numbers<br> //...</span><br>} </pre><pre class="prettyprint">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?</pre><pre class="prettyprint">Any help would be greatly appreciated.</pre><pre class="prettyprint">Thanks, Bill N&nbsp;</pre> 2007-06-22T15:24:22-04:001768477http://forums.asp.net/p/1125257/1768477.aspx/1?Re+datagrid+Items+Count+to+GridView+Rows+CountRe: datagrid.Items.Count to GridView.Rows.Count <p>I gave it a try, it seems to work for me.&nbsp; 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?</p> 2007-06-22T17:37:24-04:001770398http://forums.asp.net/p/1125257/1770398.aspx/1?Re+datagrid+Items+Count+to+GridView+Rows+CountRe: datagrid.Items.Count to GridView.Rows.Count <p>Hi Bill,</p> <p>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.</p> <p>You have to make sure that DataBind() method is called before you check for row count.</p> 2007-06-25T07:25:49-04:001771117http://forums.asp.net/p/1125257/1771117.aspx/1?Re+datagrid+Items+Count+to+GridView+Rows+CountRe: datagrid.Items.Count to GridView.Rows.Count <p>&nbsp;Thanks for the reply.</p> <p>Yes it is a button click event that triggers the code.&nbsp; When I do check even the GridView columns count it is 0 so I'm losing the data.&nbsp; The confusing part though is that this code worked in asp.net 1.1.&nbsp; The only difference is that I'm using AJAX (<a href="http://ajax.asp.net/" target="_blank">http://ajax.asp.net/</a>).</p> <p>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:<br> &nbsp;</p> <pre class="prettyprint">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(&quot;txtDay&quot;)); TextBox txt_PremiseID = (TextBox)(grdRow.FindControl(&quot;txtPremiseID&quot;)); TextBox txt_M1 = (TextBox)(grdRow.FindControl(&quot;txtM1&quot;)); 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[&quot;CurrentError&quot;] = ex.Message; Server.Transfer(&quot;Errorpage.aspx&quot;); } return dt1; }</pre> <p>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.</p> <p>Any help would be greatly appreciated.</p> <p>&nbsp;Thanks, Bill N<br> &nbsp;</p> 2007-06-25T14:31:03-04:001774663http://forums.asp.net/p/1125257/1774663.aspx/1?Re+datagrid+Items+Count+to+GridView+Rows+CountRe: datagrid.Items.Count to GridView.Rows.Count <p>Hi,</p> <p>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.</p> 2007-06-27T07:40:12-04:001774805http://forums.asp.net/p/1125257/1774805.aspx/1?Re+datagrid+Items+Count+to+GridView+Rows+CountRe: datagrid.Items.Count to GridView.Rows.Count <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Kevin Yu - MSFT</h4> <p>Hi,</p> <p>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.</p> <p></p> </blockquote> <p></p> <p>Utter nonsense. Not all data is cached in ViewState by default or otherwise.</p> 2007-06-27T09:11:36-04:001775486http://forums.asp.net/p/1125257/1775486.aspx/1?Re+datagrid+Items+Count+to+GridView+Rows+CountRe: datagrid.Items.Count to GridView.Rows.Count <p>&nbsp;Okay figured out the problem (unfortunately not the solution).&nbsp; I'm using <a href="http://www.codeproject.com/useritems/create_template_columns.asp?df=100&amp;forumid=281019&amp;exp=0&amp;select=1528051" target="_blank"> dynamic template</a> in my GridView.&nbsp; (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.&nbsp; I don't know why.</p> <p>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.</p> <p>Any help would be appreciated, Bill N&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2007-06-27T15:21:33-04:001776806http://forums.asp.net/p/1125257/1776806.aspx/1?Re+datagrid+Items+Count+to+GridView+Rows+CountRe: datagrid.Items.Count to GridView.Rows.Count <p>Hi Bill,</p> <p>I assume that the item templates were not re-created after post back. Is this what you mean in your last post?</p> <p>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.</p> <p>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!</p> 2007-06-28T07:18:29-04:001776947http://forums.asp.net/p/1125257/1776947.aspx/1?Re+datagrid+Items+Count+to+GridView+Rows+CountRe: datagrid.Items.Count to GridView.Rows.Count <p>&nbsp;</p> Could I please see your complete code in order to help you further. 2007-06-28T08:41:07-04:00