Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I am using Datagridview: Now i have do Auto increment function for a FIRST column and every cells at every RowsAdded event how i can do this please help me
Posted
Updated 12-Dec-11 2:33am
v4
Comments
Code 89 12-Dec-11 2:03am    
elaborate your question,post some of ur code..

XML
<td width=10% class="bkgd-quaternary"><font "header15-s">
                                 <%#Container.DataItemIndex+1 %>
                                 <font></td
 
Share this answer
 
if your application in Windows based follow the code

C#
public void AutoNumberRowsForGridView(DataGridView dataGridView)
{
  if (dataGridView != null)
  {
     for (int count = 0;(count <= (dataGridView.Rows.Count - 2)); count++)
     {
         dataGridView.Rows[count].HeaderCell.Value = string.Format((count + 1).ToString(), "0");
     }
  }
}

if it is webbased then follow following code
add Tempate field and itemTempalte in gridview and then assign value to column.

XML
<asp:TemplateField HeaderText="S.No." ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Container.DataItemIndex+1%>
</ItemTemplate>
</asp:TemplateField>
 
Share this answer
 
Comments
Antonis Zalonis 4-Jul-13 4:46am    
What about GridView Paggings?? I have 50 rows per page and 16 pages. With the Container.DataItemIndex+1 alwase I take from 1 to 50. How can make to works woth pages?
CHill60 4-Jul-13 9:49am    
If you have a question of your own you will get a better response by asking the question in the open forum where more people can see it
Member 10249373 24-Jan-14 10:51am    
Thanks !
Hope it helps you..

Label1.Text = "Columns with AutoIncrement Property in DataTable....<br />";  
        for (int i=0; i< dt.Columns.Count; i++)  
        {  
            Label1.Text += dt.Columns[i].ColumnName + " || ";  
            Label1.Text += dt.Columns[i].AutoIncrement + "";  
        }  
    }  


this code is, just an example pls go thru the link for further clarifications


Look at this blog u may get an idea..

AutoIncrement
 
Share this answer
 
v2
HTML
<h1>asdfadsf </h1>
 
Share this answer
 
I found this solutions and works for me

XML
<asp:TemplateField HeaderText="S.No." ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<![CDATA[<%#(Container.DataItemIndex + 1) + ((YourGridViewID.PageIndex) * (YourGridViewID.PageSize))%>]]>
</ItemTemplate>
</asp:TemplateField>
 
Share this answer
 
Comments
Antonis Zalonis 4-Jul-13 5:01am    
Sorry without <![CDATA[ ]]>
CHill60 4-Jul-13 9:48am    
The question is over a year old

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900