Gridview.Rows.Count not incremented after call to GridView.Rows.AddAt()

Hi All,

I've got a grid view that was created from a static DataTable.  I then wish to add a row to the gridview.   However the GridView.Rows.Count does not increment after I add a row to the underlying Table extracted as gv.Controls[0] as Table.  I need the gridview to maintain the correct number of rows because a post processing routine then accesses the newly addrows throught the gridview.Rows[i].Cells[j] calls.




static protected void AddRow(GridView gv, out string sOut)
	Table t = gv.Controls[0] as Table;		
	GridViewRow r = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
	TableCell cell = new TableCell();
	cell.Text = "Test";

	int iGvBefore = gv.Rows.Count;
	int iTableBefore = t.Rows.Count;

	t.Rows.AddAt(gv.Rows.Count, r); // Add The row
	int iGvAfter = gv.Rows.Count;

	int iTableAfter = t.Rows.Count;

	sOut = "Rows[gv_before, gv_after, table_before, table_after] = " +
		iGvBefore + "," + iGvAfter + "," + iTableBefore + "," + iTableAfter; 
	System.Diagnostics.Debug.Assert(iTableAfter == iTableBefore + 1);
	System.Diagnostics.Debug.Assert(iGvAfter == iGvBefore + 1);	// assert fails
8/25/2008 9:51:00 PM 72751 articles. 3 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 57

Hi crokusek,

You add row dynamically, so after each postback the row will lose. The count of rows will be always the original one. I am not quite sure about it.

You can try something else to add row:

This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


Qin Dian Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
8/28/2008 3:20:48 AM

Hi Qin,

Thanks for responding.  I tried to make my test case as simple as possible so that it is clear what does not seem to be working.  I just wanted to verify whether the behavior is a bug or if I am doing something wrong.



8/30/2008 12:43:34 AM

Hi crokusek,

I find the count has changed in table.Rows.Count, but not in GridView.Rows.Count. It is strange to me, too. However, I cannot say it is a bug. I suggest you can send your issue to MicroSoft Connect to report:


Qin Dian Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
9/1/2008 3:08:50 AM

Similar Artilces:

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Data Controls insert rows into gridview then insert rows from gridview into SQL Table
Hello there, I'm still new at performing miracles with code . Currently I am having trouble with my latest coding adventure I have a web application which records jobs and labour assigned to jobs. The issue in particular I'm having is the way in which labour is to be assigned. I want to use standard controls (Textbox, Dropdownlist) to record the values into a gridview (ie,put multiple rows into the gridview using the controls). Then, when all the inserting is complete, the user clicks a submit button to insert all the rows from the gridview into a database table. The reason for...

gridview row numbers or row count
In my gridview I want to display the number for each row, so this column doesn't have any relation with any datasource. I want it to display the number of row that it represents, so if I delete a row it still has count and represents the right number of rows that I have in my gridview. Any suggestions are welcome also. I think this thread might have what you're looking for: Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with:

GridView.Rows.Count after deleting rows
How do I get the count of GridView's data rows that is displaying?If I use GridView.Rows.Count, after deleting a row, it won't change - as same as before deleting, and nor do GridView.DataKeys.Count.  My blog (zh-CN)Be and awear of who you are. Most likely, you'll have to re-bind the data after the deletion. If so, go ahead and retrieve your count during the DataBound event, then you'll know you're retrieving an accurate count.Thanks, EdMicrosoft MVP - ASP/ASP.NET Yes, that's the easiest way to do this.I finally chose another way: in DataBound event, record the count, and in&nb...

Selecting a gridview row and keeping the last selected row in a gridview highlighted after mouse moves over a new row.
What is the best way to show a gridview row highlighted once selected and keep it highlighted once the mouse moves over a different row? Thanks, Steve hi,you can use <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="#FFFFFF" /> plz let me know if i misunderstood you Thanks for your help. One problem; however, is that after the row has been selected and I check the "grid.SelectedIndex" it has a value of -1 and not the index of the row that was selected on the grid. What do you recommend? Thanks, Steve hi, sorry i din...

Error with Gridview when i delete the row that have only one row in gridview
 Dear all, i develop with 2005 and use gridview and set enable deleteing =true. I can delete record in gridview properly but when gridveiw has only one row and then i'm clike delete button , the error occurs :   Server Error in '/it' Application. Specified argument was out of the range of valid values.Parameter name: value Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeExce...

new row below selected row in gridview to display child gridview
Hello,   I have a gridview that up selected..shows the nested child grivdview. The problem I currently have is that it stuffs all the data into a column whcih blows the whole tbale out of whack. What I need to do is create a new row below the sleected row to house the new child grivdview. I have seen third party controls do this such as teleriks radgrid but I wont to do this with the gridview control.  Any articles on how to do this? Does AJAX support this? Any help is greatly appreciated.    ...

GridView.Rows(Column=ID_PK) to GridView.Rows(Column=DropDownListTeamplteField)
Hi Guys,I wonder how I can assign the PrimaryKey in each GridView Row to the template field in the same row. whereas the template field is filled with a dropdownlist thatshould show the values for retrieved in a different table through the GridView Row primary key (which serves as the SELECT parameter to get these values) The GridView looks like this:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="C_ID" DataSourceID="GetC_IDs"> <Columns> <asp:BoundField D...

How to find a outer gridview row when a button in inner gridview row is clicked
Hi, I have a gridview within a gridview. In the inner gridview, I have a column of imagebuttons. I cannot figure out how to find out the row of the outer gridview when I click on the imagebutton in the inner gridview. I modifies the code below to get rid of some of the unnecessary stuff. <asp:GridView ID="SitePrepMainGV" runat="server" DataKeyNames="SitePrepItemID" DataSourceID="SitePrepMasterDS" OnRowDataBound="SitePrepMainGV_RowDataBound" OnRowEditing="SitePrepMainGV_RowEditing" OnRowCreated="SitePrepMainGV_RowCreated"> <Columns> <asp:BoundField DataField="S...

Using a row selection in a gridview to create database rows for another gridview
Hi there, I have 2 gridviews on a form, the first displays some template rows from a database table.  The second displays some user specific rows (with additional data added to the template info) from the same table. They both work fine for displaying the info.  Now what I want to do, is allow users to 'select' a template row on the first gridview, and have that action automatically: create a new user-specific row, with added fields from profile data etc. insert it into the table then refresh the second gridview in order to show the new added row. C...

How to automatically copy the value of a gridview row to a field in the gridview footer row
Hello,  How to automatically copy the existing value displayed in a gridview row to a field in the gridview footer row.  Both fields are of the same type.  The gridbew row is used to modify information and the footer row is used to add new information to the db. Gridview1.FindControl("WorkOrderID) GridView1.FooterRow.FindControl("NewWorkOrderID")   Thank you,ptown check this link..,Punithkumar 1 - turn column into template 2 - Add a label to footer template 3 - Say yo...

GridView Rows.Count Returns 0 When There Are Rows Visible
 Hi, I have this gridview displaying a number of rows of data (from database) on page load. The problem happens when I want to process the data. I make an if condition of if (gvReturnPackage.Rows.Count > 0) {..... }but the program won't go into the if clause as the Count property returns 0 (but the gridview actually displays those data) in debug mode.Why does this happen? Please help. Thank You. Hi make sure that you are calling if statement after  gvReturnPackage.DataBind(); method. post your code so we can monitor it.SuyogI am just using ...

Gridview with paging enable Gridview.rows.count is equal to PageSize
I am trying to cycle a GridView's rows to select a given value. I will use GridView.selectedindex. To do so I am passing a querystring varialble USERID. Next I am cycling the rows to retrieve the value to compare against so that I may get the index of the row. Problem is that the GridView Paging is enabled and has 1,2,3 at the bottom with a pagesize of 10. Therefore ==> UserMainGridView.Rows.Count; gives me 10????????   How can I get the total rows and cycle them all...perhaps my item to select is on page 3 or 5 or 77Joël Hébert [MVP ASP.NET]Opulent ASP Development Inc.www.o...

How can i copy one row or more from 1 gridview to the same gridview and other gridview ?
Hi All,      Hi, i am using visual web developer 2008 express, i need to copy some rows in a grid view to the same gridview and other gridview help me with the code when press copy. Thanks. My code : code Behind: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; using System.T...

Web resources about - Gridview.Rows.Count not incremented after call to GridView.Rows.AddAt() -

Resources last updated: 12/21/2015 4:37:24 PM