在DataGrid的第一列自动生成序号

  DataGrid的第一列自动生成序号,如下图:

     代码实现:

前台(WebForm1.aspx):

<asp:DataGrid id="grdCustomer" style="Z-INDEX: 102; LEFT: 30px; POSITION: absolute; TOP: 152px" runat="server" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" Font-Size="X-Small" AutoGenerateColumns="False">

<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999">

</SelectedItemStyle>

<AlternatingItemStyle BackColor="#FFF2F2"></AlternatingItemStyle>

<ItemStyle ForeColor="#000066" BackColor="#FAFFF9"></ItemStyle>

<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>

<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>

<Columns>

<asp:TemplateColumn HeaderText="序号">

<ItemTemplate>

<asp:label id="lable1" runat=server><%#GetCount()%></asp:label>

</ItemTemplate>

</asp:TemplateColumn>

<asp:BoundColumn DataField="CustomerID" HeaderText="客户ID"></asp:BoundColumn>

<asp:BoundColumn DataField="CompanyName" HeaderText="公司名称"></asp:BoundColumn>

<asp:BoundColumn DataField="City" HeaderText="城市"></asp:BoundColumn>

<asp:BoundColumn DataField="Address" HeaderText="地址"></asp:BoundColumn>

</Columns>

</asp:DataGrid>

 

 

 

后台(WebForm1.aspx.cs):

int count;

private void Page_Load(object sender, System.EventArgs e)

{

     if(!IsPostBack)

     {

SqlConnection cnn = new SqlConnection();

cnn.ConnectionString = "data source=localhost;initial catalog=Northwind;password=;"

         +"persist security info=True;user id=sa;workstation id=APJ062;packet size=4096";

          string sqlstr = "select Top 10 CustomerID, CompanyName, City, Address from Customers";

         cnn.Open();

         SqlDataAdapter ad = new SqlDataAdapter(sqlstr,cnn);

         DataTable dt = new DataTable();

         ad.Fill(dt);

grdCustomer.DataSource = dt;

         grdCustomer.DataBind();

     }

//自动记数函数,在前台调用

public int GetCount()

{

     return ++count ;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C# WPF中,使用MVVM模式来实现DataGrid自动添加行序号是比较常见的需求。下面是一种实现方式: 1. 首先,在ViewModel中定义一个ObservableCollection来存储数据源,并在构造函数中初始化该集合。 2. 在XAML中,使用DataGrid控件绑定到ViewModel中的数据源集合,并设置AutoGenerateColumns为False。 3. 在DataGrid的列定义中,添加一个新的列,用于显示行序号。可以使用DataGridTextColumn,并设置Binding为"{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=Items.IndexOf(.)+1}"。 4. 这样,当数据源集合发生变化时,DataGrid会自动更新行序号。 下面是一个示例代码: ViewModel.cs: ```csharp public class ViewModel : INotifyPropertyChanged { private ObservableCollection<Item> items; public ObservableCollection<Item> Items { get { return items; } set { items = value; OnPropertyChanged(nameof(Items)); } } public ViewModel() { Items = new ObservableCollection<Item>(); // 初始化数据源集合 } // INotifyPropertyChanged接口实现代码省略... } ``` MainWindow.xaml: ```xaml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp" Title="MainWindow" Height="450" Width="800"> <Grid> <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="#" Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=Items.IndexOf(.)+1}" /> <!-- 其他列定义 --> </DataGrid.Columns> </DataGrid> </Grid> </Window> ``` 在这个示例中,ViewModel类中的Items属性是用来存储数据源的ObservableCollection。在MainWindow.xaml中,使用DataGrid控件绑定到Items属性,并添加一个新的列来显示行序号

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值