0
votes

I want to add or updates rows in a blank DataGridView that is bind to a DataTable.

My DataTable has 3 column FName, LName, Number.

Also I have 3 TextBox for entering data of those columns and a Button of AddOrUpdate.

The add or update operation should follow this rule (in pseudo code):

If the item has been added before and already exists in DataTable Then
    Update the value of 'Number' Column .
ELSE 
    Add new row using given values.

And here is my code to do so:

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if ((row.Cells[1].Value.ToString().Equals(txt_Name.Text)) && (row.Cells[2].Value.ToString().Equals(txt_Family.Text)))
    {           
        row.Cells[3].Value = txt_Number.Text;
        dataGridView1.DataSource = dt;          
        break;
    }
    else
    {           
        dt.Rows.Add(txt_Code.Text, txt_Name.Text, txt_Family.Text, txt_Number.Text);
        dataGridView1.DataSource = dt;
        break;          
    }
}

Using my code, there are two problems:

  1. First row could not be added by below code (no error). (First row should be added by another button or default value.)
  2. Compare to find a existent row similar to the row being added, played only the first row of the DataGridView.

How can I do search in DatTable and perform and Add Or Update?

1
You should know what is done by each single line of code (mainly in a so small sample). Note that a DataGridView contains information in two different ways (inter-related, but different): the cells (e.g., row.Cells[1].Value) and the datasource (dataGridView1.DataSource = dt). Your code does not make too much sense: you are firstly modifying a value in a given cell and, right in the next line, changing the datasource (and thus modifying this cell again, together with all the other ones). Do some online research/testing and understand how DataGridView works before using it.varocarbas
Very tanks, but: if you correct my code, I will understand it. please help by code!hadi mohammadi
This is not the way to learn. I recommend you to look for a small working code (MSDN is full of samples) and to perfectly understand how it does everything. Once you get this step right, you can perform small modifications to accomplish increasing different actions. Also asking about each simple problem you find is the best for learning; testing many things and making lots of mistakes by your own sound much better. In any case, SO is expected to be used in other way, take a look at the help center.varocarbas

1 Answers

0
votes

I was looking this(Without DataTable):

try
{
    for (int i = 0; i < dataGridView1.RowCount; i++)
    {
        if (dataGridView1.Rows[i].Cells[0].Value.ToString() == txt_FName.Text)
        {
            dataGridView1.Rows[i].Cells[2].Value = [any proccess] ; // Update Row
            return;
        }
    }
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}
dataGridView1.Rows.Add(txt_FName.Text, txt_LName.Text, txt_Number.Text); // New Row