每次单击按钮时,如何在不同列的一行中添加数据

时间:2015-08-14 15:56:17

标签: c# visual-studio-2013 datagridview row

我正在制作计划表格,使用DataGridView我想显示该时间的计划,这将在整个星期保持一致。每列中唯一会改变的是分配给它的类。除此之外,时间保持不变。 到目前为止,每次单击按钮时,它都会将输入的数据添加到下一列的新行中。 如何使用从comboBox中选择的指定列输入同一行中的每个数据?

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++;
        }

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您将尝试在编辑行中获取下拉列表的选定值,然后将其添加到下一行。您需要获取该列中的控件,然后将其值发送到您用于保存回数据库的任何值。我不确定你有什么细胞,但用适当的细胞替换下面的细胞[0]。

var selectedVal = ((DropDownList)dgv1.Rows[rowcount].Cells[0].FindControl("yourControlID")).SelectedValue;