这是一个演示项目。
我想要实现的是,当单击一个按钮时,它将检查某些条件,然后将行插入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();
}
}
}
这是我的另一种方法,但我得到了 错误:输入数组长于此表中的列数。我在哪里弄错了?
答案 0 :(得分:0)
嗯,每个按钮单击都会清除数据表...也许用其他函数定义数据表并手动将行添加到数据表中?
答案 1 :(得分:0)
我将在评论中向您展示代码中的一些代码和解释的示例,请看一下:
首先,构建以下表格:
这是它背后的代码:
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);
}
}
}