PDA

View Full Version : راهنمایی در مورد خطای Rows cannot be programmatically added to



Iman7228
شنبه 18 آذر 1396, 22:17 عصر
سلام اساتید عزیز.
در مورد این خطا کمک میخام :

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound

میدونم که نمیشه به دیتاگریدی که متصل هست نمیشه سطر اضافه کرد، منتها در نوشتن کدی که بشه از طریق دیتاتیبل سطر اضافه کرد رو مشکل دارم.
ممنون میشم راهنمایی کنید.

ناگفته نماند که این مشکل رو با استفاده از دیتاگریدویوی کمکی حل کردم. اما میخام با دیتاتیبل مشکلم حل بشه.ممنون


public void load_database()
{


scm.Connection = scn;
scm.CommandText = "SELECT tarikh,sabt FROM tbl1 where id=@p1";
scm.Parameters.Clear();
scm.Parameters.AddWithValue("@p1", txt1.Text);
ds.Tables.Clear();
SqlDataAdapter dal = new SqlDataAdapter(scm);
dal.Fill(ds, "TEMP");
dataGridViewX1.DataSource = ds.Tables["TEMP"];


for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
{
dataGridViewX1.Rows[i].Cells[0].Value = i + 1;


}
}


private void RegBTN_Click(object sender, EventArgs e)
{


if (txt1.Text == "" || txtDate.Value == "" || txt2.Text == "")
{
MessageBox.Show("فیلدی خالی نباشد", "خطا");
}


else
{
if (txt3.Text == "0" || txt3.Text == "")
{
dataGridViewX1.Rows.Clear();


for (int i = 0; i < Convert.ToInt32(txt2.Text); i++)
{




var index = dataGridViewX1.Rows.Add();
dataGridViewX1.Rows[index].Cells[1].Value = Shamsi.DateCalculation(txtDate.Value, Shamsi.PeriodType.Month, i);
dataGridViewX1.Rows[index].Cells[2].Value = txt1.Text.Trim();
}
for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
{
dataGridViewX1.Rows[i].Cells[0].Value = i + 1;


}
buttonX1.Enabled = true;
}


else
{
// dataGridViewX1.Rows.Clear();
dataGridViewX1.Rows.Add();
dataGridViewX1.Rows[0].Cells[1].Value = txtDate.Value;
dataGridViewX1.Rows[0].Cells[2].Value = txt3.Text;
for (int i = 1; i < Convert.ToInt32(txt2.Text); i++)
{
var index = dataGridViewX1.Rows.Add();


dataGridViewX1.Rows[index].Cells[1].Value = Shamsi.DateCalculation(txtDate.Value, Shamsi.PeriodType.Month, i);
dataGridViewX1.Rows[index].Cells[2].Value = txt1.Text.Trim();
}
for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
{
dataGridViewX1.Rows[i].Cells[0].Value = i + 1;


}
buttonX1.Enabled = true;
}
}




}




private void DaftarFrm_Activated(object sender, EventArgs e)
{
load_database();
for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
{
dataGridViewX1.Rows[i].Cells[0].Value = i + 1;


}
}