hello guys.

i would like to ask ur opinion on how to solve my problem. i would like to add a new column that will generate autonumber to the existing datagrid. based on my research i got from google, i only manage to create the new column(but it is empty). hope someone will help me to solve my problem. here's my code. please have a look and guide me.

<asp:DataGrid id="DataGrid1" runat="server" BorderStyle="None" AutoGenerateColumns="False" Width="950px" BackColor="White" BorderColor="#3366CC" BorderWidth="1px" CellPadding="4" HorizontalAlign="Center" EmptyDataText="There are no data records to display." EnableTheming="True" DataSourceID="BrokerRanking" Font-Names="calibri" Font-Size="16px">
<HeaderStyle ForeColor="White" />
<Columns>
<asp:TemplateColumn HeaderText="No">
<HeaderStyle BackColor="#2B1B17" Font-Bold="True" HorizontalAlign="Center" Font-Names="calibri" Font-Size="16px" Width="10px" />
<ItemStyle BackColor="White" Font-Bold="False" Font-Italic="False" Font-Names="Calibri"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" ForeColor="Black" />

</asp:TemplateColumn>

<asp:BoundColumn DataField="PName" ReadOnly="True" HeaderText="Name" DataFormatString=" {0:N2}">
<HeaderStyle BackColor="#2B1B17" Font-Bold="True" HorizontalAlign="Center" Font-Names="calibri" Font-Size="16px" Width="150px" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" HorizontalAlign="Left" />
</asp:BoundColumn>
<asp:BoundColumn DataField="totalVolume" ReadOnly="True" HeaderText="Vol ('mil)" DataFormatString=" {0:N2}">
<HeaderStyle BackColor="#2B1B17" Font-Bold="True" HorizontalAlign="Center" Font-Names="calibri" Font-Size="16px" Width="100px" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" HorizontalAlign="Right" />
</asp:BoundColumn>
<asp:BoundColumn DataField="PercentVolume" ReadOnly="True" HeaderText="%" DataFormatString=" {0:N2}">
<HeaderStyle BackColor="#2B1B17" Font-Bold="True" HorizontalAlign="Center" Font-Names="calibri" Font-Size="16px" Width="100px" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" HorizontalAlign="Right" />
</asp:BoundColumn>

</Columns>

</asp:DataGrid>

Recommended Answers

All 8 Replies

First of all you should mention the language you are using is it VB.NET

if yes then here is the code

Private Sub DataGridView1_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserAddedRow
        If DataGridView1.Rows(e.Row.Index).IsNewRow Then
            If e.Row.Index = 1 Then
                DataGridView1.Rows(e.Row.Index - 1).Cells(0).Value = e.Row.Index
            Else
                DataGridView1.Rows(e.Row.Index - 1).Cells(0).Value = DataGridView1.Rows(e.Row.Index - 2).Cells(0).Value + 1
            End If
        End If
    End Sub

i'm sorry Naveed_786. forgot to mention that. yes, it is in vb.net and thank you for replying. :)

First of all you should mention the language you are using is it VB.NET

if yes then here is the code

Private Sub DataGridView1_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserAddedRow
        If DataGridView1.Rows(e.Row.Index).IsNewRow Then
            If e.Row.Index = 1 Then
                DataGridView1.Rows(e.Row.Index - 1).Cells(0).Value = e.Row.Index
            Else
                DataGridView1.Rows(e.Row.Index - 1).Cells(0).Value = DataGridView1.Rows(e.Row.Index - 2).Cells(0).Value + 1
            End If
        End If
    End Sub

The code u gave is 'DataGridView1' but i'm using 'DataGrid1'. I thought they are different things and somehow it cause errors and it still does not generate the autonumber. One of the errors is; Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
How should i fix this? thanks.

The code u gave is 'DataGridView1' but i'm using 'DataGrid1'. I thought they are different things and somehow it cause errors and it still does not generate the autonumber. One of the errors is; Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
How should i fix this? thanks.

Ignore DatagridView1 or Datagrid1. Thats just the name of the control so to make it right, put the correct name given to your datagrid control there.

Ignore DatagridView1 or Datagrid1. Thats just the name of the control so to make it right, put the correct name given to your datagrid control there.

just want to clarify, 'name given to your datagrid control' that you have mentioned earlier referring to the '<asp:DataGrid id="..(here is the datagrid control name)...">'

correct me if i'm wrong. i am new in vb and still need to learn more from you guys. huhu

Are you sure you are using vb.net as i know there is only one name by default which is datagridview1 if you have changed the name of datagridview1 to datagrid1 then just replace datagridview1 with datagrid1

Are you sure you are using vb.net as i know there is only one name by default which is datagridview1 if you have changed the name of datagridview1 to datagrid1 then just replace datagridview1 with datagrid1

erm, actually i just got a message from the moderator that this thread has been moved to ASP.NET. i guess i have mistakenly thought it is vb.net >_<

i have tried changing the datagridview1 to datagrid1 but got errors;
1. 'Rows' is not a member of 'System.Web.UI.WebControls.DataGrid'

i have found the solution guys. just add in these simple line to the column and it will generate the numbers accordingly. here's the code;

<asp:TemplateColumn HeaderText="No">
<HeaderStyle BackColor="#2B1B17" Font-Bold="True" HorizontalAlign="Center" Font-Names="calibri" Font-Size="16px" Width="10px" />
<ItemStyle BackColor="White" Font-Bold="False" Font-Italic="False" Font-Names="Calibri"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" ForeColor="Black" />

//here's the code that will generate the numbers
<itemtemplate>                    
    <%# (DataGrid1.PageSize*DataGrid1.CurrentPageIndex)+ Container.ItemIndex+1%> 
</itemtemplate>   

</asp:TemplateColumn>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.