C#中用DateGridView显示数据库如下,如果要加一个按钮,名为保存,一按按钮可以保存DateGridView中的数据
C#中用DateGridView显示数据库,设置一个按钮,使其已被按下,就能保存DateGridView被修改的数据,要怎么编。注:数据库名:xtepstore表名:pr...
C#中用DateGridView显示数据库,设置一个按钮,使其已被按下,就能保存DateGridView被修改的数据,要怎么编。
注:数据库名:xtepstore 表名:products 展开
注:数据库名:xtepstore 表名:products 展开
2个回答
展开全部
右击控件,添加列,将类型设置为datagridviewbuttoncolumn。页眉也自己设置,一般是删除或者是修改。
这是按钮点击事件的代码,你可以参考下
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (Form1.type == 0)
{
if (e.ColumnIndex == 0)
{
this.Height = 471;
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
textBox5.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
comboBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
}
else if (e.ColumnIndex == 1)
{
if (MessageBox.Show("确实要删除吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
try
{
String ss = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
scn.Open();
SqlCommand scm = new SqlCommand("delete from BookInf where BookId='" + ss + "'");
scm.Connection = scn;
int i = scm.ExecuteNonQuery();
if (i != 0)
{
MessageBox.Show("删除成功!", "提示");
}
else
{
MessageBox.Show("删除失败!", "提示");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
SqlDataAdapter sdd = new SqlDataAdapter("select * from BookInf", scn);
DataTable dt = new DataTable();
sdd.Fill(dt);
dataGridView1.DataSource = dt;
scn.Close();
}
}
}
}
}
这是按钮点击事件的代码,你可以参考下
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (Form1.type == 0)
{
if (e.ColumnIndex == 0)
{
this.Height = 471;
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
textBox5.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
comboBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
}
else if (e.ColumnIndex == 1)
{
if (MessageBox.Show("确实要删除吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
try
{
String ss = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
scn.Open();
SqlCommand scm = new SqlCommand("delete from BookInf where BookId='" + ss + "'");
scm.Connection = scn;
int i = scm.ExecuteNonQuery();
if (i != 0)
{
MessageBox.Show("删除成功!", "提示");
}
else
{
MessageBox.Show("删除失败!", "提示");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
SqlDataAdapter sdd = new SqlDataAdapter("select * from BookInf", scn);
DataTable dt = new DataTable();
sdd.Fill(dt);
dataGridView1.DataSource = dt;
scn.Close();
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询