簡體   English   中英

如何在C#中的同一Datagridview中使用現有行添加新的Datagridview行

[英]How to add new Datagridview rows with the existing rows in the same Datagridview in C#

這是一個演示項目。

我想要實現的是,當單擊按鈕時,它將檢查某些條件,然后將行插入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();
        }
        }            
    }

這是我的另一種方法,但出現錯誤:輸入數組的長度大於此表中的列數。 我在哪里犯錯?

嗯,每次單擊按鈕都會清除數據表...也許用其他功能定義數據表並手動向數據表添加行?

我將向您展示一個示例,其中的代碼帶有一些代碼和注釋,請看一下:

首先,構建以下表格:

形成

這是其背后的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM