Witam, mam problem z pobieraniem wierszy do DataGridView, przy pierwszy pobraniu nie ma problemu natomiast przy odświeżeniu dostaję błąd referencji przy this.Rows.Add(), ? Problem znika gdy usunę linijkę this.Rows.Clear(), ale przez to wiersze się dublują? Poniżej zamieszczam kod metody GetRow(), Probowałem jeszcze zamiast Clear() dać
for (int i = 0; i < this.RowCount; )
{
this.Rows.RemoveAt(i);
}
, ale efekt był taki sam. Object references not set to an instance of an object.
private void GetRows()
{
this.stopFormating = true;
SqlConnection connection = new SqlConnection(mySQL.connectionString);
SqlCommand command = new SqlCommand(this.sqlCommandString, connection);
SqlDataReader reader;
this.Rows.Clear();
try
{
connection.Open();
reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string read = "";
this.Rows.Add();
foreach (myTextBox t in lTbOtrzymane)
{
read = reader[t.Name].ToString();
if (t.IsMoney)
{
Base.ValidatingTextBoxMoney(ref read);
}
if (t.TypeColumn == "Image")
{
this.Rows[RowCount - 1].Cells[t.Name].Value = image16x16.Images[Convert.ToInt32(reader[t.Name].ToString())];
}
if (t.TypeColumn == "TextBox")
{
this.Rows[RowCount - 1].Cells[t.Name].Value = read;
}
if (t.TypeColumn == "Calendar")
{
this.Rows[RowCount - 1].Cells[t.Name].Value = read;
this.Columns[t.Name].ValueType = typeof(CalendarColumn);
bool a = this.Columns[t.Name].ReadOnly;
}
}
}
}
reader.Close();
this.lblCountRow.Text = this.RowCount.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
this.stopFormating = false;
}