0

I Have 1 Datatable having 10 rows and ListBox having 8 ListItems contains 6 records from the DataTable and 2 new records.

I want to update the DataTable in such a way that 6 records should be as it is and remove remaining 4 records from DataTable and add 2 newly added entries from ListBox in DataTable.

What I tried is I looped ListBox record from DataTable and created list of matched records.

string impactedTC;
List<int> index = new List<int>();
// This retruns my dataset having 10 records           
DataTable dttable = GetImpactedTestCaseDetailsToUpdateStatus().Tables[0];

for (int i = 0; i < ListBox1.Items.Count; i++)
{
    int count = 0;

    string dTestCase = ListBox1.Items[i].Text;
    foreach (DataRow dtRow in dttable.Rows)
    {
        impactedTC = dtRow["TestCaseName"].ToString();
        if (impactedTC == dTestCase)
        {
            index.Add(count);
        }
        count++;
    }
}

1 Answers1

0

You can do that using Ling:

To keep the 6 rows and remove the remaining 4 from the DataTable:

//Assuming the names are DataTable1 and ListBox1.
var rowsToRemove = from r in DataTable1.Rows.Cast<DataRow>()
        where listBox1.Items
        .Cast<ListItem>()
        .Aggregate(0, (n, li) => li.Text.ToLower() == r.Field<string>("TestCaseName").ToLower() ? n + 1 : n) == 0
        select r;

To get the new items from the ListBox:

var newItems = from li in listBox1.Items.Cast<ListItem>()
                where DataTable1.Rows
                .Cast<DataRow>()
                .Aggregate(0, (n, r) => r.Field<string>("TestCaseName").ToLower() == li.Text.ToLower() ? n + 1 : n) == 0
                select li;

and finally update the DataTable:

rowsToRemove.ToList().ForEach(r => DataTable1.Rows.Remove(r));
newItems.ToList().ForEach(li => DataTable1.Rows.Add(li.Text)); //or maybe li.Value

Important

You might need to replace any li.Text with li.Value in the preceding code and that depends on how the ListItem objects are created. Please check this for more details.

  • Thanks @JQSOFT I do have ListItem so struggling to to add new item into datatable. Could you please help me in this? 'System.Web.UI.WebControls.ListItem' to type 'System.IConvertible' error occurred during execution. can you help me in this. – prashanat chaudhari Dec 31 '19 at 06:14
  • @prashanatchaudhar Of course it will throw that exception since it is a different type. You had to make your post more clear by adding the right tags, the type of your project,.. etc. Anyway, The same logic will work if you change the types and call the correct properties. Try the edited code, –  Dec 31 '19 at 09:24
  • @prashanatchaudhar Also check what you are really need to get, is it the [ListItem.Text](https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listitem.text?view=netframework-4.8#System_Web_UI_WebControls_ListItem_Text) property or the [ListItem.Value](https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listitem.value?view=netframework-4.8#System_Web_UI_WebControls_ListItem_Value)? –  Dec 31 '19 at 09:34
  • ListItem.Text is required. I am new to linq i am still struggling with this. Could you please help me with the code? @JQSOFT – prashanat chaudhari Dec 31 '19 at 10:28
  • @prashanatchaudhar I Just did mate, check the code. I replaced the `string` with `ListItem`, and I removed the alternative function since it won't work here. Try it and tell me what you get. –  Dec 31 '19 at 10:44
  • thanks. review view has expected values however adding into DataTable using Li.Text getting 'Input string was not in a correct format.' error and for li.value i blank values added into DataTable. – prashanat chaudhari Dec 31 '19 at 11:00
  • Also i want to ask rowsToRemove.ToList().ForEach(r => DataTable1.Rows.Remove(r)); rowsToRemove.ToList() i want to remove this from database using my in build function. is there any way to call my function from Linq syntax? – prashanat chaudhari Dec 31 '19 at 11:07
  • This is what i tried foreach(DataRow dr in rowsToRemove.ToList()) { int Count = 0; string testcase = dr.Table.Rows[Count]["TestCaseName"].ToString(); RemoveTCFromDataBase(testcase); Count++; } – prashanat chaudhari Dec 31 '19 at 11:10