DataGrid和GridView自动增加序号(三种实现方式)

1 篇文章 0 订阅
1 篇文章 0 订阅

DataGrid自动增加序号(三种实现方式)

方法一:,直接在Aspx页面DataGrid模板列中.

缺点是到第二页分页时又重新开始了



<asp:TemplateColumn HeaderText="序号" FooterText="  " HeaderStyle-Height="20px" FooterStyle-Height="20px" HeaderStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" >

    <ItemTemplate>

       <%#Container.DataSetIndex+1 %>

</ItemTemplate>

    <HeaderStyle Width="50px" />

    <ItemStyle HorizontalAlign="Center" /></asp:TemplateColumn>


方法二:分页时进行了页面计算,这样会累计向下加. 

<asp:TemplateColumn HeaderText="序号" FooterText="  " HeaderStyle-Height="20px" FooterStyle-Height="20px" HeaderStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" >

    <ItemTemplate>

    <asp:Label ID="id" runat="server" Text='<%# this.dgGift.CurrentPageIndex*this.dgGift.PageSize+this.dgGift.Items.Count+1 %>' />

    </ItemTemplate>

    <HeaderStyle Width="50px" />

    <ItemStyle HorizontalAlign="Center" />

    </asp:TemplateColumn>
方法三:放在cs代码中,和第二种相似. 

<asp:TemplateColumn HeaderText="序号" FooterText="  " HeaderStyle-Height="20px" FooterStyle-Height="20px" HeaderStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" > <HeaderStyle Width="50px" />

<ItemStyle HorizontalAlign="Center" />

 <asp:Label ID="id" runat="server" />

 

    </asp:TemplateColumn>


protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e) 

if (this.DataGrid1.Items.Count != 0

int indexID = this.DataGrid1.CurrentPageIndex*this.DataGrid1.PageSize+this.DataGrid1.Items.Count+1; 
(this.DataGrid1.Items[i].FindControl("id") as Label).Text = indexID.ToString(); 

 

GridView自动增加序号(三种实现方式)

方法一:,直接在Aspx页面GridView模板列中.

缺点是到第二页分页时又重新开始了

<asp:TemplateField HeaderText="序号" InsertVisible="False"> 
<ItemTemplate> 
<%#Container.DataItemIndex+1%> 
</ItemTemplate> 
</asp:TemplateField> 

 


方法二:分页时进行了页面计算,这样会累计向下加. 

<asp:TemplateField HeaderText="序号" InsertVisible="False"> 
<ItemStyle HorizontalAlign="Center" /> 
<HeaderStyle HorizontalAlign="Center"/> 
<ItemTemplate> 
<asp:Label ID="Label2" runat="server" Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>' /> 
</ItemTemplate> 
</asp:TemplateField> 


方法三:放在cs代码中,和第二种相似. 

<asp:BoundField HeaderText="序号" ></asp:BoundField> 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 

if (e.Row.RowIndex != -1) 

int indexID = this.GridView1.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1; 
e.Row.Cells[0].Text = indexID.ToString(); 

在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、付费专栏及课程。

余额充值