locked
How to check whether row exits in datatable or not? RRS feed

  • Question

  • Hello,

    I'm working on a condition where it adds a new row if row is not available in a datatable and performs different actions as required.

    if (Name == dt[w]["Title"].ToString())

    {

    // some actions

    }

    else

    {

    // some actions

    }

    However, it throws error if row does not exist.

    I cannot use dt.rows.count since it restricts me to enter inside the logic and add another row.

    I would like to know how to control this validation


    Sohaib Khan

    Friday, May 5, 2017 2:02 AM

Answers

  • Hi Sohaib Khan,

    Thank you for posting here.

    For your question, please refer to the following code.

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            DataTable dataTable = new DataTable();
            private void Form1_Load(object sender, EventArgs e)
            {
                dataTable.Columns.Add("ID");
                dataTable.Columns.Add("Title");
                dataTable.Rows.Add(1, "C#");
                dataTable.Rows.Add(2, "Java");
                dataGridView1.DataSource = dataTable;
            }
            private void button1_Click(object sender, EventArgs e)
            {           
                string ID = textBox1.Text;
                string title = textBox2.Text;
                DataRowCollection dt = dataTable.Rows;
                int count = dt.Count;
                for (int w = 0; w <= count; w++)
                {
                    if (w < count)//row is available 
                    {
                        if (ID == dt[w]["ID"].ToString() && title == dt[w]["Title"].ToString())
                        {
                            MessageBox.Show("this row is exist!");
                            break;
                            // some actions
    
                        }
                    }
                    else
                    {
                        // adds a new row
                        dataTable.Rows.Add(ID, title);
                        MessageBox.Show("add successs");
                    }
                }
            }
        }

    I make a gif to show result.

    I could not make sure of what yours title, I create ID and Title. And when I add new rows, if the ID and Title are all not exist, it will add a new row.

    I hope this would be helpful. If you have something else about this issue, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sohaib Khan Sunday, May 7, 2017 7:11 PM
    Friday, May 5, 2017 7:11 AM