如何在每次单击按钮时在不同列的一行中添加数据
本文关键字:数据 添加 一行 单击 按钮 | 更新日期: 2023-09-27 18:07:54
我正在制作时间表形式,使用DataGridView我想要显示该时间的时间表,这将是一致的整个星期。每列中唯一会改变的是分配给它的类。除此之外,时间保持不变。到目前为止,每次单击该按钮时,它都会将输入的数据添加到下一列的新行中。如何在从组合框中选择指定列的同一行中输入每个数据?
private void button1_Click_1(object sender, EventArgs e)
{
dgv1.Rows.Add(1);
if (comboBox1.Text == "Monday")
{
DataGridViewRow row = dgv1.Rows[rowcount];
row.Height = 60;
grpclassname = textBox1.Text;
txtinst = cmbInstructor.Text;
//txtinst = textBox2.Text;
txtdesc = textBox3.Text;
txt = grpclassname + "'n" + txtinst + "'n" + txtdesc;
dgv1.Rows[rowcount].Cells[0].Value = tp1.Value.TimeOfDay.Hours.ToString() + ":" + tp1.Value.TimeOfDay.Minutes + "-" + tp2.Value.TimeOfDay.Hours + ":" + tp2.Value.TimeOfDay.Minutes;
dgv1.Rows[rowcount].Cells[1].Value = grpclassname + "'n" + txtinst + "'n" + txtdesc;
rowcount++;
}
else if (comboBox1.Text == "Tuesday")
{
DataGridViewRow row = dgv1.Rows[rowcount];
row.Height = 60;
grpclassname = textBox1.Text;
txtinst = cmbInstructor.Text;
//txtinst = textBox2.Text;
txtdesc = textBox3.Text;
txt = grpclassname + "'n" + txtinst + "'n" + txtdesc;
dgv1.Rows[rowcount].Cells[0].Value = tp1.Value.TimeOfDay.Hours.ToString() + ":" + tp1.Value.TimeOfDay.Minutes + "-" + tp2.Value.TimeOfDay.Hours + ":" + tp2.Value.TimeOfDay.Minutes;
dgv1.Rows[rowcount].Cells[2].Value = grpclassname + "'n" + txtinst + "'n" + txtdesc;
rowcount++;
}
如果我正确理解你的问题,你正试图在编辑行中获得下拉列表的选定值,然后将其添加到下一行。你需要得到那个列中的控件然后把它的值发送给你用来保存回数据库的任何东西。我不确定你在哪个单元格,但用适当的单元格替换下面的Cells[0]。
var selectedVal = ((DropDownList)dgv1.Rows[rowcount].Cells[0].FindControl("yourControlID")).SelectedValue;