Click here to Skip to main content
15,886,847 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Comments
Jibesh 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
k@ran 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

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[^]
 
Share this answer
 
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"));
 
Share this answer
 
Comments
Victor Rene 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<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();
C#
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;
        }
    }
}
 
Share this answer
 
v3
Comments
k@ran 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"));

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