0

I have one datatable which has

DataTable:

         SeqNo        ItemID        ItemName
        -------       ------        --------
           1            10           AAA
           2            20           BBB
           3            30           CCC

Now I want to remove the Row which has the SeqNo "3".

Now I am doing like this in GridView RowCommand Event:

    if (e.CommandName == "Delete")
        {
            string SeqNo = e.CommandArgument.ToString();
            for (int i = 0; i < DTItem.Rows.Count; i++)
            {
                if (SeqNo == DTItem.Rows[i]["SeqNo"].ToString())
                {
                    DTItem.Rows.Remove(DTItem.Rows[i]);
                }
            }
        }

But without loops, How to remove the row based on this condition?

1
  • Why no loops? Obviously loops will be used at some point - even if it's within the library code you call. You can't implement an operation on a collection without a loop of some kind at some point. Commented Aug 18, 2011 at 10:23

4 Answers 4

2

try

DataRow[] rows = DTItem.Select(" SeqNo = " + SeqNo );
rows[0].Delete();
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

DataRow[] rows = DTItem.Select("SeqNo = " + SeqNo);
foreach (DataRow row in rows) {
    DTItem.Rows.Remove(row);
}

Comments

0

If you are sure that there is only one and definitely one occurrence of the row you can achieve that as following

dt1.Rows.Remove(dt1.Select("SeqNo= 3")[0]);

Comments

0

Another option is to use DataView instance.

//Set sort key and order
 DTItem.DefaultView.Sort = "SeqNo";
 DTItem.DefaultView.Delete(DTItem.DefaultView.Find(2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.