TechGuy
TechGuy

Reputation: 4560

Delete some data on dataset C#

I want to delete some data from the dataset not the whole row.

Ex: NewsNo | RID | REMARKS
            1         100   L1,R1,R2,R3

In here i want to delete L1 data only.

My Code

 DataSet dSet = new DataSet();
 ExcelAdapter.Fill(dSet);
 dSet.Tables[0].Rows[i].Delete(); //<-- It delete whole row.i just want to delete L1 only.

Upvotes: 1

Views: 425

Answers (4)

Gubr
Gubr

Reputation: 324

you should update the value of cell (like you would do in a database). in your case the to remove L1 it would look something like this

dSet.Tables[0].Rows[i] = string.join(",",dSet.Tables[0].Rows[i].ToString().Split(',').RemoveAt(0));

Upvotes: 1

Alex Art.
Alex Art.

Reputation: 8781

In this case you need to do update of remarks column with the value that doesn't contain L1

Assuming that L1 is a value that you are getting dynamically (for example as user input) I think it could be something like that :

var valToRemove = "L1"; //provided by user
var remarks = dSet.Tables[0].Rows[i]["REMARKS"];
var splittedRemarks = new List<string>(remarks.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries));
splittedRemarks.Remove(valToRemove); //If you need to remove all the L1 values use `RemoveAll` method instead
dSet.Tables[0].Rows[i]["REMARKS"] = splittedRemarks.Join(",");

Upvotes: 1

Shanid
Shanid

Reputation: 587

You can do this :

string str = dSet.Tables[0].Rows[i]["REMARKS"].toString();
dSet.Tables[0].Rows[i]["REMARKS"] = str.Remove(str.IndexOf("L1"), 3);

Here, index of L1 is found and 3 characters are removed starting from that index.

Upvotes: 0

Lesmian
Lesmian

Reputation: 3952

You want to update data not delete it...:

dSet.Tables[0].Rows[i]["REMARKS"] = "R1,R2,R3";

Upvotes: 1

Related Questions