【问题标题】:How to add new Datagridview rows with the existing rows in the same Datagridview in C#如何在 C# 中的同一 Datagridview 中添加新的 Datagridview 行和现有行
【发布时间】:2016-01-30 01:49:11
【问题描述】:

这是一个演示项目。

我想要实现的是,当单击一个按钮时,它会检查某些条件,然后将行插入 Datagridview。

问题是我需要再次插入行中存在的现有行 datagridview,应用了单选按钮条件(新行和更新)。

到目前为止,我已经取得了这么多。

private void button5_Click(object sender, EventArgs e)
    {
        if (button5.Text == "")
        {
            MessageBox.Show("No Value Assigned");
        }
        else
        {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
        con.Open();
        MySqlDataAdapter dt = new MySqlDataAdapter();
        DataTable tt = new DataTable();
        BindingSource bs = new BindingSource();
        if (dataGridView1.RowCount > 0)
        {                
            bs = (BindingSource)dataGridView1.DataSource;
        }
        else
        { 
                if (radioButton1.Checked == true)
                {

                    string query = "SELECT type,priceS FROM service WHERE type='" + button5.Text + "'";
                    MySqlCommand cmd = new MySqlCommand(query, con);
                    dt.SelectCommand = cmd;                       
                }
                else if (radioButton2.Checked == true)
                {
                    string query = "SELECT type,priceM FROM service WHERE type='" + button5.Text + "'";
                    MySqlCommand cmd = new MySqlCommand(query, con);
                    dt.SelectCommand = cmd;

                }
                else if (radioButton3.Checked == true)
                {
                    string query = "SELECT type,priceL FROM service WHERE type='" + button5.Text + "'";
                    MySqlCommand cmd = new MySqlCommand(query, con);
                    dt.SelectCommand = cmd;                        
                }
            }            
        dt.Fill(tt);
        bs.DataSource = tt;
        dataGridView1.DataSource = bs;
        dt.Update(tt);
        con.Close();
        }            
    }

我可以插入第一行,但是当我尝试插入另一个具有相同按钮的问题时。

任何想法都值得赞赏。

private void button5_Click(object sender, EventArgs e)
    {
        if (button5.Text == "")
        {
            MessageBox.Show("No Value Assigned");
        }
        else
        {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
        con.Open();
        MySqlDataAdapter dt = new MySqlDataAdapter();
        DataTable tt = new DataTable();
        BindingSource bs = new BindingSource();
        if (radioButton1.Checked == true)
        {

            string query = "SELECT type,priceS FROM service WHERE type='" + button5.Text + "'";
            MySqlCommand cmd = new MySqlCommand(query, con);
            dt.SelectCommand = cmd;
        }
        else if (radioButton2.Checked == true)
        {
            string query = "SELECT type,priceM FROM service WHERE type='" + button5.Text + "'";
            MySqlCommand cmd = new MySqlCommand(query, con);
            dt.SelectCommand = cmd;

        }
        else if (radioButton3.Checked == true)
        {
            string query = "SELECT type,priceL FROM service WHERE type='" + button5.Text + "'";
            MySqlCommand cmd = new MySqlCommand(query, con);
            dt.SelectCommand = cmd;
        }
        if (dataGridView1.RowCount > 0)
        {
            tt.Rows.Add(dataGridView1);
            dt.Fill(tt);
            bs.DataSource = tt;
            dataGridView1.DataSource = bs;
            dt.Update(tt);
            con.Close();
        }
        else
        {
            dt.Fill(tt);
            bs.DataSource = tt;
            dataGridView1.DataSource = bs;
            dt.Update(tt);
            con.Close();
        }
        }            
    }

这是我的另一种方法,但我得到了 错误:输入数组长于此表中的列数。我在哪里做错了?

【问题讨论】:

  • 您是否尝试过调试以查看三种不同的 Radio 检查会发生什么。这就是我要开始的地方。

标签: c# mysql datagridview


【解决方案1】:

我给你看个例子,里面有代码,cmet里面有一些代码和解释,看一下:

首先,构建如下表单:

下面是它的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Initially your DataGridView will not have a DataSource to display your data so you need
            // to define a DataTable that will be used as the DataSource for you DataGridView
            var dt = new DataTable();
            // Add the desired columns to it, with their headers and data types
            dt.Columns.Add(new DataColumn("Id", typeof(int)));
            dt.Columns.Add(new DataColumn("Description", typeof(string)));

            // You can add new rows to your DataTable using the DataTable object itself
            // in order to create DataRows with the exact DataColumns required for that DataTable
            var newRow = dt.NewRow();
            newRow["Id"] = 1;
            newRow["Description"] = "Desc 1";
            // And then you simply add this new row to your DataTable
            dt.Rows.Add(newRow);

            newRow = dt.NewRow();
            newRow["Id"] = 2;
            newRow["Description"] = "Desc 2";
            dt.Rows.Add(newRow);

            newRow = dt.NewRow();
            newRow["Id"] = 3;
            newRow["Description"] = "Desc 3";
            dt.Rows.Add(newRow);

            // By the end you set the DataSource property of your DataGridView
            // assigning to it the DataTable you created
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // In order to add new rows to your DataGridView with the current DataTable
            // you simply need to take it back from the DataGridView.DataSource property by casting it
            // to a DataTable again
            var dt = (DataTable)dataGridView1.DataSource;

            // And then you add the new rows in the same way that were shown in the Click event for button1
            var newRow = dt.NewRow();
            newRow["Id"] = 4;
            newRow["Description"] = "Desc 4";
            dt.Rows.Add(newRow);

            newRow = dt.NewRow();
            newRow["Id"] = 5;
            newRow["Description"] = "Desc 5";
            dt.Rows.Add(newRow);
        }
    }
}

【讨论】:

    【解决方案2】:

    嗯,每次点击按钮都会清除数据表...也许用其他功能定义数据表并手动向数据表添加行?

    【讨论】:

    • 请详细一点,告诉我如何用一些编码添加新行...我不是专家
    • 我想你不明白我的问题,看看当按钮被点击它检查是否有行,如果现在有它基于单选按钮条件的插入,现在如果有(这个问题开始的地方)我需要用新键入的值插入现有的......如果(dataGridView1.RowCount> 0)请检查这个条件。如果你能告诉我我的问题出在哪里......
    猜你喜欢
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2012-09-17
    相关资源
    最近更新 更多