Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how to remove data from data table from one index and insert into another.
 
I have three field which i am binding to datatable
 
in_website_id website_name login_name
 
5 vvv ggg
6 vvv ggg
1 vvv ggg
2 vvv ggg
8 vvv ggg
 

inthis datatable i want to remove data of in_website_id (1,2) from 3rd row and insert into 1 and 2nd row..
 
How to do this plzzz help ??
Posted 8-Feb-13 9:16am
Comments
jibesh at 8-Feb-13 14:43pm
   
'i want to remove data of in_website_id (1,2) from 3rd row and insert into 1 and 2nd row.'
This statement is confusing can you make it simple by putting the expected output from above dataTable
mrinal_kumar_jha at 8-Feb-13 15:08pm
   
want to remove row which has id 1 and insert this row at 1st position.
like
5 vvv ggg
6 vvv ggg
1 vvv ggg
2 vvv ggg
8 vvv ggg
 
should be
1 vvv ggg
5 vvv ggg
6 vvv ggg
2 vvv ggg
8 vvv ggg
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

using System.Collections.Generic;
using System.Data;
using System.Linq;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create test table
            DataTable dt = CreateTestTable("TestTable");
 
            // Cast to IEnumerable and sort
            IEnumerable<datarow> dataRows = dt.Rows.Cast<datarow>();
            var sortedRows = from DataRow r in dataRows
                       orderby r["in_website_id"]
                       select r;
 
            // Put back in
            dt = sortedRows.CopyToDataTable();
        }
 
        private static DataTable CreateTestTable(string tableName)
        {
            // Create a test DataTable with two columns and a few rows.
            DataTable table = new DataTable(tableName);
 
            DataColumn column = new DataColumn("in_website_id", typeof(System.Int32));
            table.Columns.Add(column);
            
            column = new DataColumn("website_name", typeof(System.String));
            table.Columns.Add(column);
 
            column = new DataColumn("login_name", typeof(System.String));
            table.Columns.Add(column);
 
            // rows.
            DataRow row1 = table.NewRow();
            row1["in_website_id"] = 5;
            row1["website_name"] = "vvv";
            row1["login_name"] = "ggg";
            table.Rows.Add(row1);
 
            DataRow row2 = table.NewRow();
            row2["in_website_id"] = 6;
            row2["website_name"] = "vvv";
            row2["login_name"] = "ggg";
            table.Rows.Add(row2);
 
            DataRow row3 = table.NewRow();
            row3["in_website_id"] = 1;
            row3["website_name"] = "vvv";
            row3["login_name"] = "ggg";
            table.Rows.Add(row3);
 
            DataRow row4 = table.NewRow();
            row4["in_website_id"] = 2;
            row4["website_name"] = "vvv";
            row4["login_name"] = "ggg";
            table.Rows.Add(row4);
 
            DataRow row5 = table.NewRow();
            row5["in_website_id"] = 8;
            row5["website_name"] = "vvv";
            row5["login_name"] = "ggg";
            table.Rows.Add(row5);
 
            table.AcceptChanges();
            return table;
        }
    }
}
  Permalink  
v3
Comments
mrinal_kumar_jha at 8-Feb-13 15:54pm
   
Got the solution check this..
 
if (dtWebsite.Rows.Count > 0)
{
DataRow[] drWebsite = dtWebsite.Select("in_website_id in (1,604)");
int iRowIndex = 0;
foreach (DataRow drWebsite1 in drWebsite)
{
DataRow drWebsite2 = dtWebsite.NewRow();
drWebsite2.ItemArray = drWebsite[iRowIndex].ItemArray;
dtWebsite.Rows.Remove(drWebsite[iRowIndex]);
dtWebsite.Rows.InsertAt(drWebsite2, 0);
iRowIndex ++;
}
ddlWebsite.DataSource = dtWebsite;
ddlWebsite.DataTextField = "vc_website_name";
ddlWebsite.DataValueField = "in_website_id";
ddlWebsite.DataBind();
ddlWebsite.Items.Insert(0, new ListItem("Select Website", "0"));
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Got the solution...Its working..
 
if (dtWebsite.Rows.Count > 0)
{
DataRow[] drWebsite = dtWebsite.Select("in_website_id in (1,604)");
int iRowIndex = 0;
foreach (DataRow drWebsite1 in drWebsite)
{
DataRow drWebsite2 = dtWebsite.NewRow();
drWebsite2.ItemArray = drWebsite[iRowIndex].ItemArray;
dtWebsite.Rows.Remove(drWebsite[iRowIndex]);
dtWebsite.Rows.InsertAt(drWebsite2, 0);
iRowIndex ++;
}
ddlWebsite.DataSource = dtWebsite;
ddlWebsite.DataTextField = "vc_website_name";
ddlWebsite.DataValueField = "in_website_id";
ddlWebsite.DataBind();
ddlWebsite.Items.Insert(0, new ListItem("Select Website", "0"));
  Permalink  
Comments
Victor Rene at 8-Feb-13 16:50pm
   
It should be working. You create a new row, copy the fields, delete first row, insert new row at first row and then increment row index.
 
But I'm a lazy person. I use LINQ.
// Cast to IEnumerable and sort
IEnumerable dataRows = dt.Rows.Cast();
var sortedRows = from DataRow r in dataRows
orderby r["in_website_id"]
select r;

// Put back in
dt = sortedRows.CopyToDataTable();
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

This answer stackoverflow discusses a problem very much similar to your problem: http://stackoverflow.com/questions/3150216/data-table-delete-a-row-in-c-sharp-using-loop[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100