【发布时间】: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