[英]How to insert data to the next row in datagridview using c#?
Good day! 美好的一天!
Please help me on my concern. 请帮助我。 I have a code here which allows to add rows to the datagridview I created named dgvSchedule. 我这里有一个代码,该代码允许向我创建的名为dgvSchedule的datagridview添加行。 I want to transfer data from the other datagridview dgvCalendar by double clicking the data in the cell. 我想通过双击单元格中的数据从其他datagridview dgvCalendar传输数据。 My problem is I can only add data on the first row in dgvSchedule and the next rows are empty. 我的问题是我只能在dgvSchedule的第一行添加数据,而下一行为空。
I have attached screenshot also of my application. 我还附有我的应用程序的屏幕截图。
Thank you. 谢谢。
private void dgvCalendar_DoubleClick(object sender, EventArgs e)
{
if (cmbTimeSched.Text != "")
{
string yearnow = DateTime.Now.Year.ToString();
int year = Convert.ToInt32(yearnow);
int rowIndex = dgvCalendar.CurrentCell.RowIndex;
int colIndex = dgvCalendar.CurrentCell.ColumnIndex;
if (dgvCalendar.Columns[colIndex].HeaderText != "SUN")
{
string Daynum = dgvCalendar.Rows[rowIndex].Cells[colIndex].Value.ToString();
int dayNum = Convert.ToInt32(Daynum);
int num = DateTime.Parse("" + selectMonths.Text + year).Month;
DateTime dateSelected = new DateTime(year, num, dayNum);
dgvSchedule.Rows.Add();
dgvSchedule.Rows[0].Cells[0].Value = lblIDNum.Text;
dgvSchedule.Rows[0].Cells[1].Value = cmbTimeSched.Text;
dgvSchedule.Rows[0].Cells[2].Value = dgvCalendar.Columns[colIndex].HeaderText.ToString();
dgvSchedule.Rows[0].Cells[3].Value = dateSelected.ToString("MM/dd/yyyy");
}
else
{ MessageBox.Show("You selected a date that is on SUNDAY!"); }
}
else
{ MessageBox.Show("PLEASE SELECT TIME SCHEDULE FIRST!"); }
}
MyApplication: 我的应用程序:
Because you are specifically saying to fill the record at Row[0] in dgvSchedule table. 因为您是专门说要填充dgvSchedule表中Row [0]处的记录。
int count=dgvSchedule.Rows.Count;
dgvSchedule.Rows[count+1].Cells[0].Value = lblIDNum.Text;
dgvSchedule.Rows[count+1].Cells[1].Value = cmbTimeSched.Text;
dgvSchedule.Rows[count+1].Cells[2].Value=
dgvCalendar.Columns[colIndex].HeaderText.ToString();
dgvSchedule.Rows[count+1].Cells[3].Value = dateSelected.ToString("MM/dd/yyyy");
My other suggestion is you can use dataset to fill the grid. 我的另一个建议是您可以使用数据集填充网格。
DataTable dt1 = new DataTable();
//remove all the rows of table
while (dgvSchedule.Rows.Count > 1)
{
dgvSchedule.Rows.RemoveAt(0);
}
//fill the table with updated records
cnn.Open();
MySqlDataAdapter adp = new MySqlDataAdapter("query for fetch the results from data base which you need to fill in the table", cnn);
adp.Fill(dt1);
dgvSchedule.DataSource = dt1;
adp.Dispose();
cnn.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.