Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Dear Friends

Can you please tell me how to set serial Number in Data Gridview at run time means at runtime how many row i insert Serial number get also +1;



thanks:
Posted

C#
/add a column for serial no
 
DataSet ds = new DataSet();
DataColumn c = new DataColumn();
c.ColumnName = "Serial No.";
c.DataType = typeof(int);
ds.Tables.Add("TableA");        //add a table
ds.Tables[0].Columns.Add(c);             //add the column
ds.Tables[0].Columns[0].AutoIncrement = true;     //set the autoincrement (it starts from 0)
 
//this two lines add a blank row for the serial no '0' and remove it
//that way the serial number starts from 1 , instead of 0 for the fetched records.
ds.Tables[0].Rows.Add(0);
ds.Tables[0].Rows[0].Delete();
//after this you can upload the fetched records in a regular way (please refer "TableA" and dataset ds though) (see this codeguru.com link: http://www.codeguru.com/forum/showthread.php?t=452168 "  (How to display large number of rows in a DataGridView?)
 
Share this answer
 
Have a look at this....
Generate serial number in gridview [^]
 
Share this answer
 
use this template field in your gridview

<asp:gridview id="grdAdd" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown">

<columns>
XML
<asp:TemplateField HeaderText="SrNo.">                                                           <ItemTemplate>                                                               <%#Container.DataItemIndex+1%>                                                </ItemTemplate>
<ItemStyle Width="8%" />                                                           </asp:TemplateField>

</asp:gridview>


if your problem will soved please accept my answer
 
Share this answer
 
v2

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