如何实现对 System.Windows.Forms.DataGridView的增加、修改、上除操作 RT 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 datagridView 本身就提供的有这些功能.只需要设置一些熟悉就可以了.AllowUserToAddRows,AllowUserToDeleteRows,这些设置为true.查询只有自己写代码了.可以做成Excel的数据筛选的效果也可以自己定义查询的编辑器来查询.如果是绑定到数据库的那就可以通过设置SQL语句从新查询一次然后再重新绑定到控件上. private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True"); SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa); DataSet ds = new DataSet(); SqlCommandBuilder scbld = new SqlCommandBuilder(sda); //sc.Open(); sda.Fill(ds, "temp"); //ds.Tables["temp"].Rows[0]["name"] = "sss"; //int rows=sda.Update(ds, "temp"); int rowindex = e.RowIndex; int columnindex = e.ColumnIndex; //label1.Text = rowindex.ToString() + "行"+columnindex.ToString()+"列"; //string str = Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].t); if (rowindex > -1 && columnindex > -1) { ds.Tables["temp"].Rows[rowindex]["name"] = dataGridView1[1,rowindex].Value.ToString(); ds.Tables["temp"].Rows[rowindex]["age"] = dataGridView1[0,rowindex].Value; ds.Tables["temp"].Rows[rowindex]["zip"] = dataGridView1[2,rowindex].Value.ToString(); //label1.Text = rowindex.ToString() + "行"+columnindex.ToString()+"列"+dataGridView1[rowindex, columnindex].Value.ToString(); //label1.Text = dataGridView1[0, rowindex].ValueType.ToString(); } sda.Update(ds, "temp"); //this.shitTableAdapter.Fill(this.masterDataSet1.shit); //sa.Close(); } //add new item... private void button1_Click(object sender, EventArgs e) { SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True"); SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa); DataSet ds = new DataSet(); SqlCommandBuilder scbld = new SqlCommandBuilder(sda); sda.Fill(ds, "temp"); DataRow dr = ds.Tables["temp"].NewRow(); dr["name"] = textBox1.Text; dr["age"] = Convert.ToInt32(textBox2.Text); dr["city"] = textBox3.Text; dr["zip"] = textBox4.Text; ds.Tables["temp"].Rows.Add(dr); sda.Update(ds, "temp"); this.shitTableAdapter.Fill(this.masterDataSet1.shit); shitBindingSource.DataSource = masterDataSet1; dataGridView1.DataSource = shitBindingSource; } private void dataGridView1_SelectionChanged(object sender, EventArgs e) { } private void dataGridView1_Click(object sender, EventArgs e) { SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True"); SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa); DataSet ds = new DataSet(); SqlCommandBuilder scbld = new SqlCommandBuilder(sda); sda.Fill(ds, "temp"); //DataRow dr = ds.Tables["temp"].NewRow(); string str = dataGridView1.SelectedRows[0].Cells["nameDataGridViewTextBoxColumn"].Value.ToString(); str1 = "name = " + "'" + str + "'"; //DataRow[] dr = ds.Tables["temp"].Select(str1); //if(dr.Length>0) //label1.Text = dr[0]["name"].ToString(); //dr[0].Delete(); } private void button2_Click(object sender, EventArgs e) { SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True"); SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa); DataSet ds = new DataSet(); SqlCommandBuilder scbld = new SqlCommandBuilder(sda); sda.Fill(ds, "temp"); DataRow[] dr = ds.Tables["temp"].Select(str1); foreach (DataRow datarow in dr) { datarow.Delete(); } sda.Update(ds, "temp"); this.shitTableAdapter.Fill(this.masterDataSet1.shit); shitBindingSource.DataSource = masterDataSet1; dataGridView1.DataSource = shitBindingSource; //dataGridView1.Refresh(); }我自己的,希望对你有用,表中有四列,name(char),age(int),zip(char),city(char) 补充一下,private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)用来更改datagridview中的数据 private void button1_Click(object sender, EventArgs e)是用在外面添加textbox的方法来改数据 private void dataGridView1_Click(object sender, EventArgs e)和 private void button2_Click(object sender, EventArgs e)它们组合起来可以删除一行,datagridview_click把选中行的“name”传给str;str1是select的条件button2_Click完成删除 DotNetBar里面的calendarView控件的使用问题 (做值班表) 求教个问题。 怎么判断该update了还是数据没有变? if(!bl)!的作用是什么?(菜鸟提问) 委托、事件的奇怪问题! 请教一个MD5加密的问题 急,vb翻译成c#问题,马上结贴 IE缓存怎么消除? 哪位大哥算法好来帮看一下 C#的事务处理怎么对建表和删表不管用啊??? 大家好 60分跪求:Asp.net 网站打包问题!
AllowUserToAddRows,AllowUserToDeleteRows,这些设置为true.
查询只有自己写代码了.可以做成Excel的数据筛选的效果也可以自己定义查询的编辑器来查询.
如果是绑定到数据库的那就可以通过设置SQL语句从新查询一次然后再重新绑定到控件上.
{
SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa);
DataSet ds = new DataSet();
SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
//sc.Open();
sda.Fill(ds, "temp");
//ds.Tables["temp"].Rows[0]["name"] = "sss";
//int rows=sda.Update(ds, "temp");
int rowindex = e.RowIndex;
int columnindex = e.ColumnIndex;
//label1.Text = rowindex.ToString() + "行"+columnindex.ToString()+"列";
//string str = Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].t);
if (rowindex > -1 && columnindex > -1)
{
ds.Tables["temp"].Rows[rowindex]["name"] = dataGridView1[1,rowindex].Value.ToString();
ds.Tables["temp"].Rows[rowindex]["age"] = dataGridView1[0,rowindex].Value;
ds.Tables["temp"].Rows[rowindex]["zip"] = dataGridView1[2,rowindex].Value.ToString();
//label1.Text = rowindex.ToString() + "行"+columnindex.ToString()+"列"+dataGridView1[rowindex, columnindex].Value.ToString();
//label1.Text = dataGridView1[0, rowindex].ValueType.ToString();
}
sda.Update(ds, "temp");
//this.shitTableAdapter.Fill(this.masterDataSet1.shit); //sa.Close();
}
//add new item...
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa);
DataSet ds = new DataSet();
SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
sda.Fill(ds, "temp");
DataRow dr = ds.Tables["temp"].NewRow();
dr["name"] = textBox1.Text;
dr["age"] = Convert.ToInt32(textBox2.Text);
dr["city"] = textBox3.Text;
dr["zip"] = textBox4.Text;
ds.Tables["temp"].Rows.Add(dr);
sda.Update(ds, "temp");
this.shitTableAdapter.Fill(this.masterDataSet1.shit);
shitBindingSource.DataSource = masterDataSet1;
dataGridView1.DataSource = shitBindingSource;
} private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
} private void dataGridView1_Click(object sender, EventArgs e)
{
SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa);
DataSet ds = new DataSet();
SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
sda.Fill(ds, "temp");
//DataRow dr = ds.Tables["temp"].NewRow();
string str = dataGridView1.SelectedRows[0].Cells["nameDataGridViewTextBoxColumn"].Value.ToString();
str1 = "name = " + "'" + str + "'";
//DataRow[] dr = ds.Tables["temp"].Select(str1);
//if(dr.Length>0)
//label1.Text = dr[0]["name"].ToString();
//dr[0].Delete();
} private void button2_Click(object sender, EventArgs e)
{
SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa);
DataSet ds = new DataSet();
SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
sda.Fill(ds, "temp");
DataRow[] dr = ds.Tables["temp"].Select(str1);
foreach (DataRow datarow in dr)
{
datarow.Delete();
}
sda.Update(ds, "temp");
this.shitTableAdapter.Fill(this.masterDataSet1.shit);
shitBindingSource.DataSource = masterDataSet1;
dataGridView1.DataSource = shitBindingSource;
//dataGridView1.Refresh();
}
我自己的,希望对你有用,表中有四列,name(char),age(int),zip(char),city(char)
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)用来更改datagridview中的数据
private void button1_Click(object sender, EventArgs e)
是用在外面添加textbox的方法来改数据
private void dataGridView1_Click(object sender, EventArgs e)和 private void button2_Click(object sender, EventArgs e)它们组合起来可以删除一行,
datagridview_click把选中行的“name”传给str;str1是select的条件
button2_Click完成删除