自动序号 [英] automatic and sequential numbers

查看:73
本文介绍了自动序号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表格上自动顺序地给C#DataGridView编号.我该怎么办?

I want to give numbers for C# DataGridView on the form automatically and sequentially. What should I do?

推荐答案

如果要基于RowIndex在DataGridView中显示行号,但又不想存储该行datasource 中的数字,然后可以使用DataGridViewVirtualMode 属性.
在此给出了在DataGridView 中实现虚拟模式的完整过程: http://msdn.microsoft.com /en-us/library/15a31akc.aspx [ ^ ]
在虚拟模式下,仅当网格在虚拟模式下运行时,才会触发CellValueNeeded 事件,并在网格首次显示时为当前在网格中可见的每个未绑定列单元格触发.当用户滚动时,如此处所示
http://www.informit.com/articles/article.aspx?p=446453& seqNum = 6 [ ^ ]
If you want to display the number of the row in the DataGridView based on the RowIndex, but do not want to store the row number in the datasource then you can use the VirtualMode property of DataGridView.
A walk through on implementing virtual mode in DataGridView is given here:http://msdn.microsoft.com/en-us/library/15a31akc.aspx[^]
In virtual mode the CellValueNeeded event will only be fired when the grid is operating in virtual mode, and will be fired for each unbound column cell that is currently visible in the grid when it is first displayed and as the user scrolls, as given here
http://www.informit.com/articles/article.aspx?p=446453&seqNum=6[^]


取决于您从何处获取数据.
如果来自SQL来源;然后只需在SELECT语句中添加一列即可:
Depends on where you get your data from.
If it comes from an SQL source; then just add a column to the SELECT statement:
SELECT ROW_NUMBER() OVER (ORDER BY Id) AS 'Row Number',Id, Description FROM myTable


<asp:GridView ID="GridView1" runat="server" DataSourceID="objCustomer">

    <Columns>

       <asp:TemplateField HeaderText="Serial No">

         <ItemTemplate>

   <asp:Label ID="lblNo" runat="server" Text='<%# Container.DataItemIndex + 1  %>'/>

</ItemTemplate>

      </asp:TemplateField>

   </Columns>

</asp:GridView>


这篇关于自动序号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆