Actually the question itself, there is a string which must be assigned a value

object _id = kontr_dg.Rows[i].Cells["id"].Value; 

at the output _id = null although the right side is equal to the value 2. Tried on another

 string _id = kontr_dg.Rows[i].Cells["id"].Value.toString(); 

same on output _id = null. Tell me what to do?
piece of code where this line is:

 MySqlDataAdapter sda = new MySqlDataAdapter("select id,name,inn,adress,type from kontr",con); DataSet ds = new DataSet(); sda.Fill(ds); kontr_dg.DataSource = ds.Tables[0]; for (int i = 0; i < kontr_dg.Rows.Count; i++) { //Название с ОПФ object _id = kontr_dg.Rows[i].Cells["id"].Value; string name_opf = core.kontr_name_opf(_id.ToString()); kontr_dg.Rows[i].Cells["name"].Value = name_opf.ToString(); } 
  • As I understand it, kontr_dg is a DataGridView. If so, its Rows [i] property must still have a reference to Row from the bound DS. Data must be taken from this Row. I am now unable to give an example, if you don’t manage it yourself - I will give it in the morning - Alexander Muksimov
  • one
    Probably, you have not disabled the ability to add a new line by the user. Or add kontr_dg.AllowUserToAddRows = false; or i < kontr_dg.Rows.Count - 1 in a loop i < kontr_dg.Rows.Count - 1 to ignore the dummy row. - Ev_Hyper

1 answer 1

The problem was that it was possible to add a new line. because of this, a null value was assigned at the end of the loop.

Right answer

 kontr_dg.AllowUserToAddRows = false; 

Prevent adding new line