为DataGrid添加自动编号功能

为DataGrid添加自动编号功

http://lucky_elove.www1.dotnetplayground.com/

下面的代码实现在DataGrid中添加自动编号的功能,主要是在数据绑定时利用Item属性。

查看例子

DataGridWithLine.aspx

  <%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataGridWithLine.aspx.vb" Inherits="aspxWeb.DataGridWithLine"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>DataGridWithLine</title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <HeaderStyle Font-Bold="True" Wrap="False" HorizontalAlign="Center"></HeaderStyle> <Columns> <asp:TemplateColumn></asp:TemplateColumn> <asp:BoundColumn DataField="Title"></asp:BoundColumn> <asp:BoundColumn DataField="CreateDate" DataFormatString="{0:yyyy-M-d h:m:s}"></asp:BoundColumn> </Columns> </asp:DataGrid> </form> </body> </HTML> 

DataGridWithLine.aspx.vb

  Imports System Imports System.Data Imports System.Data.OleDb Public Class DataGridWithLine Inherits System.Web.UI.Page Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid #Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DataGrid1.Columns(0).HeaderText = "序号" DataGrid1.Columns(1).HeaderText = "文章标题" DataGrid1.Columns(2).HeaderText = "创建日期" Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Test.mdb") Dim strSQL As String = "SELECT TOP 21 Title,CreateDate FROM Document ORDER By CreateDate DESC" Dim cn As New OleDbConnection(cnString) cn.Open() Dim cmd As New OleDbCommand(strSQL, cn) Dim db As OleDbDataReader db = cmd.ExecuteReader(CommandBehavior.CloseConnection) DataGrid1.DataSource = db DataGrid1.DataBind() cn.Close() cn = Nothing cmd = Nothing db.Close() db = Nothing End Sub Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound If e.Item.ItemIndex <> -1 Then e.Item.Cells(0).Text = e.Item.ItemIndex + 1 End If End Sub End Class 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF DataGrid可以通过设置属性来实现自动添加新行。具体步骤如下: 1. 在DataGrid控件添加一个新行: <DataGrid ItemsSource="{Binding MyData}" AutoGenerateColumns="False" CanUserAddRows="True"> <DataGrid.Columns> <DataGridTextColumn Header="Column 1" Binding="{Binding Column1}" /> <DataGridTextColumn Header="Column 2" Binding="{Binding Column2}" /> </DataGrid.Columns> </DataGrid> 2. 在ViewModel初始化数据源: public class ViewModel { public ObservableCollection<MyDataModel> MyData { get; set; } public ViewModel() { MyData = new ObservableCollection<MyDataModel>(); //添加默认行 MyData.Add(new MyDataModel()); } } 3. 在MyDataModel设置自动添加新行: public class MyDataModel : INotifyPropertyChanged { //实现INotifyPropertyChanged接口 public event PropertyChangedEventHandler PropertyChanged; //两列数据 private string _column1; public string Column1 { get { return _column1; } set { _column1 = value; RaisePropertyChanged("Column1"); //添加新行 if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(Column2)) { RaiseRequestNewRow(); } } } private string _column2; public string Column2 { get { return _column2; } set { _column2 = value; RaisePropertyChanged("Column2"); //添加新行 if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(Column1)) { RaiseRequestNewRow(); } } } //请求添加新行事件 public event EventHandler RequestNewRow; private void RaiseRequestNewRow() { EventHandler handler = RequestNewRow; if (handler != null) { handler(this, EventArgs.Empty); } } //通知数据变更 private void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } 4. 在MainWindow订阅RequestNewRow事件: public MainWindow() { InitializeComponent(); var viewModel = new ViewModel(); this.DataContext = viewModel; viewModel.MyData.CollectionChanged += MyData_CollectionChanged; } private void MyData_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { //添加新行 if (e.NewItems != null && e.NewItems.Count > 0) { var newItem = e.NewItems[0] as MyDataModel; newItem.RequestNewRow += NewItem_RequestNewRow; } } } private void NewItem_RequestNewRow(object sender, EventArgs e) { var viewModel = this.DataContext as ViewModel; if (viewModel != null) { viewModel.MyData.Add(new MyDataModel()); } } 这样设置后,当用户在最后一行输入完成后,DataGrid自动添加一个新行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值