【问题标题】:Insert multiple data via datagridView in c# with the use of a checkbox使用复选框在c#中通过datagridView插入多个数据
【发布时间】:2020-02-18 06:13:27
【问题描述】:

我这里有这段代码,一旦选中复选框,我希望通过 datagridview 将多个学生标记插入数据库。下面代码的问题是只插入了第一个选中行的数据。我该怎么做才能确保插入每个选定的行。

    public override void Savebtn_Click(object sender, EventArgs e)
    {
        int select;
        for (int i = 0; i <= dataGridView1.Rows.Count -1; i++)
        {
            DataGridViewRow row = dataGridView1.Rows[i];
            if((bool)row.Cells["CheckBoxColumn"].FormattedValue == true)
            {
                select++;
            }

        }
        if(select == 0)
        {
            MainClass.showMSG("Please Select at least one Student", "Error", "Error");
        }
        for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
        {
            DataGridViewRow row = dataGridView1.Rows[i];

            if ((bool)row.Cells["CheckBoxColumn"].FormattedValue == true)
            {
              obj.st_InsertResults(Convert.ToInt32(row.Cells["stdIDGV"].Value.ToString()), 
               row.Cells["StdNameGV"].Value.ToString(),
               Convert.ToInt32(row.Cells["EngGV"].Value.ToString()));
                MainClass.showMSG(" Data added successfully.", "Success....", "Success");
                MainClass.disable_reset(panel1);
                dataGridView1.DataSource = null;

            }
        }

    }

【问题讨论】:

    标签: c# checkbox datagridview


    【解决方案1】:

    为什么要将数据源设置为 null?

    dataGridView1.DataSource = null;
    

    尝试删除此行,应该可以!

    【讨论】:

    • 谢谢,尽管它产生了一个新问题。插入后如何引用行?因为我最初使用空数据源来这样做。
    • 我相信目标是遍历所有行并对其进行处理。为什么需要刷新行?循环应该自行完成并添加行。
    • 您可以将最后三行移出循环(在其中设置一个布尔值)并在工作完成后显示消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    相关资源
    最近更新 更多