给DataTable添加序号列

如何在datatable中添加一序号列,编号从1依次递增,并且在第一列?

/// <summary>
        /// 在DataTable中添加一序号列,编号从1依次递增
        /// </summary>
        /// <param >DataTable</param>
        /// <returns></returns>
        private DataTable AddSeriNumToDataTable(DataTable dt)
        {        
            //需要返回的值
            DataTable dtNew;
            if (dt.Columns.IndexOf("序号") >= 0)
            {
                dtNew = dt;
            }
            else //添加一序号列,并且在第一列
            {
                int rowLength = dt.Rows.Count;
                int  colLength= dt.Columns.Count;
                DataRow[] newRows = new DataRow[rowLength];

dtNew = new DataTable();
                //在第一列添加“序号”列
                dtNew.Columns.Add("序号");
                for (int i = 0; i < colLength; i++)
                {
                    dtNew.Columns.Add(dt.Columns[i].ColumnName);
                    //复制dt中的数据
                    for (int j = 0; j < rowLength; j++)
                    {
                        if (newRows[j] == null)
                            newRows[j] = dtNew.NewRow();
                        //将其他数据填充到第二列之后,因为第一列为新增的序号列
                        newRows[j][i+1] = dt.Rows[j][i];
                    }
                }
                foreach (DataRow row in newRows)
                {
                    dtNew.Rows.Add(row);
                }

}
            //对序号列填充,从1递增
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dtNew.Rows[i]["序号"] = i + 1;
            }

return dtNew;           
            
        }

private DataTable dt;
        private void Form1_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt.Columns.Add("名称");
            dt.Columns.Add("价格");

dt.Rows.Add("白菜", 12);
            dt.Rows.Add("白菜", 12);
            dt.Rows.Add("白菜", 12);
            dt.Rows.Add("白菜", 12);
            dt.Rows.Add("白菜", 12);

DataTable dt2 = AddSeriNumToDataTable(dt);
            dataGridView1.DataSource = dt2;

}

时间: 2024-10-29 10:20:48

给DataTable添加序号列的相关文章

dataTable添加序号列

使用render( data, type, row, meta )的参数meta 直接新加一个列 columns: [ {data: "id", title: "id", visible: false, searchable: false}, {data: null, tile: "序号", visible: true, searchable: false}, {data: "name", title: "名称&qu

GridView添加序号列

在GridView控件第0列添加序号列: <asp:BoundField HeaderText="序号" > <ItemStyle HorizontalAlign="Center" /> <HeaderStyle HorizontalAlign="Center" Width="30px" /> </asp:BoundField> 为GridView添加OnRowDataBound

AspxGridView添加序号列

using System.Globalization;//CultureInfo需要 protected void AspxGridView_stock_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e) { if (e.DataColumn.Caption.Trim() == "序号") { e.Cell.Text = ((e.Vis

ASP.NET 为GridView添加序号列,且支持分页连续累计显示

为GridView添加序号列,且支持分页连续累计显示,废话不多说,直接上代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T

datatable 加序号列

最近使用datatable时,发现没有像jqgrid那样生成序号列,在国外网站搜罗了一下还是很简单的,就要在aoColumns中添加一空列占位就行,然后再用fnRowCallback添加序号 示例如下 by hsuchan: //加载列表数据 tables = $('#launch_table').alijkDataTable({ "sAjaxSource" : path+'/appUseStatistics/appLaunchDetail.do?'+params, "iDi

DataTable添加行和列

DataTable dt = dal.GetDtatableadd(name, note); //// //添加新行////// DataRow drr = dt.NewRow();//新增一行 // 添加数据: //dr["ADD"] = "122313";//第一种直接将名称全部写出来添加 //第二种加结果和列名取出来添加 object[] objs = { "1", "测试", "测试公司", &qu

ASP.NET repeater添加序号列的方法

1.<itemtemplate> <tr><td> <%# Container.ItemIndex + 1%> </td></tr> </itemtemplate> 2.<itemtemplate> <tr><td> <%# this.rpResult.Items.Count + 1%> </td></tr> </itemtemplate>

RadGridView添加序号列

public class RowNumberColumn : GridViewDataColumn { public override System.Windows.FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem) { TextBlock textBlock = cell.Content as TextBlock; if (textBlo

oracle 自动添加序号列 排序

select      HSL.sortno,                    HSL.B,                    HSL.A,                    row_number() over(order by sortno desc) xh               from (select  t.B, t.A ,                            case                              when t.B = '