如何在将数据填充到数据表时继续增加行号。 [英] how to continue row number increasingly while filling data to datatable.?

查看:57
本文介绍了如何在将数据填充到数据表时继续增加行号。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i在数据库中有少量记录如下:



记录1.



i have the few records in database like follows:

Record 1.

Rowno        name

1            christ

2            william

3            Albert.



记录2.




Record 2.

Rowno       Name

1           Martin

2           Allen

3           Moorthy.




$ c $ b在c#后​​面的代码中。我使用选择查询将第一条记录绑定到表中,例如,




in code behind c#. i am binding first record into table using select query like,

sqlcommand cmd=new sqlcommand("select * from record1",con);

datatadapater adap=new dataadapter(cmd);

datatable dt=new datatable();

datatable mergedt = new datatable();

adap.fill(dt);



和,



同样我绑定第二条记录并填写另一个数据表。




And,

similarly i am binding second record and filling to another datatable.

sqlcommand cmd=new sqlcommand("select * from record1",con);

datatadapater adap=new dataadapter(cmd);

datatable secoddt = new datatable();

datatable mergedt = new datatable();

adap.fill(secoddt );





直到现在,每次都可以正常工作。



这里,我需要合并表1和表2.因此,我将表2的值合并到表1中,如下所示。



dt.merge(secoddt);



现在我在一张桌子上得到了两个桌子值的结果。



但行号如下:





Till now everyhing works fine.

Here, i need to merge the both table 1 and table 2. so, i merged table 2 value into table 1 like follows.

dt.merge(secoddt);

now i got results of two table value in one table.

but row number comes like follows:

Rowno      name

1          christ

2          william

3          Albert

1          Martin

2          Allen

3          Moorthy  // can you see the row number did not come in accending order.  it comes 1,2,3 and again it repeats 1,2,3...i need to display row number like 1,2,3,4,5,6:





结果应显示



The result should display

Row no    Name

1         christ

2         William

3         Albert

4         Martin

5         Allen

6         Moorthy.





请帮忙。



Please help.

推荐答案

实际上,你不必使用从数据库返回的行号,你可以在运行时生成行号在c#中以编程方式订购。



我使用以下代码重新创建了您的方案,您将获得正在运行的行号。





Actually, you do not have to use the row number returned from the database, you can generate the row number in running order programmatically in c#.

I have recreated your scenario using the following code and you will get the running row number.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static DataTable GetMergeTable()
        {
            //
            // to recreate your scenario
            //
            DataTable mergeTable = new DataTable();

            mergeTable.Columns.Add("rowno", typeof(int));
            mergeTable.Columns.Add("name", typeof(string));

            mergeTable.Rows.Add(1, "christ");
            mergeTable.Rows.Add(2, "william");
            mergeTable.Rows.Add(3, "albert");
            mergeTable.Rows.Add(1, "martin");
            mergeTable.Rows.Add(2, "allen");
            mergeTable.Rows.Add(3, "moorthy");

            return mergeTable;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable mergeTable = GetMergeTable();

            int i = 1;
            foreach (DataRow row in mergeTable.Rows)
            {
                row["rowno"] = i;
                i++;
            }

            dataGridView1.DataSource = mergeTable;
        }
    }
}


试试这个



http://stackoverflow.com/questions/7775923/merging-two-datatables-in -c-sharp [ ^ ]


你必须为每一行使用唯一的编号,然后你才能得到结果。
you must use unique number for each row then u will eeasily get result .


这篇关于如何在将数据填充到数据表时继续增加行号。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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