Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i have the few records in database like follows:

Record 1.
SQL
Rowno        name

1            christ

2            william

3            Albert.

Record 2.

SQL
Rowno       Name

1           Martin

2           Allen

3           Moorthy.


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

C#
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.

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

datatadapater adap=new dataadapter(cmd);

datatable secoddt = new datatable();

datatable mergedt = new datatable();

adap.fill(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:

C#
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
SQL
Row no    Name

1         christ

2         William

3         Albert

4         Martin

5         Allen

6         Moorthy.


Please help.
Posted
Updated 21-Nov-13 23:27pm
v3
Comments
thatraja 22-Nov-13 4:25am    
Do you want show these records in Gridview? Row no is just for display purpose, right?
Thanks7872 22-Nov-13 4:26am    
Where you are displaying all this results? Which control?
King Fisher 22-Nov-13 4:36am    
are you getting this row number from DB?
christhuxavier 22-Nov-13 5:08am    
hi thatraja,Rohan Leuva and King_fisher,
yes i am taking row number from the database and i did not show anywhere. i am showing records in datalist over the site. for paging concept i need row number. in datalist, there is no option for paging. we have to make paging using row number. That is why i need row number for paging.

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.


C#
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;
        }
    }
}
 
Share this answer
 
v4
Comments
christhuxavier 22-Nov-13 5:09am    
can you give example?
Peter Leow 22-Nov-13 6:21am    
I have just sent you an example.
King Fisher 22-Nov-13 6:33am    
useful.
you must use unique number for each row then u will eeasily get result .
 
Share this answer
 
Comments
christhuxavier 22-Nov-13 5:09am    
can you give example.
[no name] 22-Nov-13 5:26am    
ya sure.
You must maintain recordid by using foreign key constraints .

first insert records in record1 ,
and in record2 table for recordid create foreign key and give reference column from record1 also same for name.

try this . if not works reply me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900