Sorry I'm struggling with this, I have tried all solutions listed here, none of them worked. I'm new to C# and I need your help with this, I got people scanning items to be entered in a datagridview control. with the code below I search DVG for a value and then add a new dvg row based on if the value exist or not.
public void InnerLoadData()
{
if (textBox1.Text != string.Empty)
{
textBox1.DataBindings.Clear();
textBox2.DataBindings.Clear();
ScanID.DataBindings.Clear();
CardNumber.DataBindings.Clear();
Boolean result = true;
string searchValue = ScanID.Text;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ( row!=null &&
row.Cells [3].Value != null &&
row.Cells[3].Value.ToString() .Equals +(searchValue) &&
row.Cells [4].Value.ToString()=="OUT" &&
dataGridView1 !=null )
{
row.Selected = true;
row.SetValues(textBox1.Text,
textBox2.Text,
CardNumber.Text,
ScanID.Text,
"IN",
DateTime .Now );
row.DefaultCellStyle.BackColorColor.Green ;
result = false;
break ;
}
}
if (result !=false )
{
this.dataGridView1.Rows.Add(textBox1.Text,
textBox2.Text,
CardNumber.Text,
ScanID.Text,
"OUT",
DateTime.Now); **// got error here**
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red ;
}
}
}
Here is my problem,
I save all the datagridview data to an sql table on FormClosing just in case, when I reload the data from sql and try adding new datagridview rows, I got the message above. Any idea how to fix it ? thank you.
private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
{
BindingSource bsdata1 = new BindingSource();
dataGridView1 .AutoGenerateColumns = false;
dataGridView1 .AutoGenerateColumns = false;
//DataGridView1.DataSource = Nothing
using (SqlCommand cmd = new SqlCommand("get_badge", _econnect))
{ cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt1);
bsdata1.DataSource = dt1;
dataGridView1 .DataSource = bsdata1 ;
}
}}}}