c# 关于 DataGridView 大家谁有例子程序吗? 谢谢 DataGridView 非绑定连sql填充,删除,添加,修改的程序例子有吗, 我搜了半天,没有全的。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我这有份DataGridView操作合集,要的话可以发给你!留个方式要是不好你也别怪我! http://www.cnblogs.com/gentlewolf/archive/2007/09/07/886053.html 填充直接用DataTable赋给他啊至于删除某行:if (this.DataGridView1.CurrentRow != null) { if (!DataGridView1.Rows[this.DataGridView1.CurrentRow.Index].IsNewRow) { this.DataGridView1.Rows.Remove(DataGridView1.Rows[this.DataGridView1.CurrentRow.Index]); BigMessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } }添加和修改某行信息:直接在下列事件中处理就行了,保存时把DataGridView1里的信息保存在起来就好了private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e){} 楼主去微软下官方的例子:http://windowsclient.net/downloads/folders/applications/default.aspx OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\zhengshuangliang\\全代码自移测试\\RADotNet\\Pages\\USM\\jingyou.mdb;"); olecon.Open(); string comm = "select * from [jingyou]"; OleDbDataAdapter odda = new OleDbDataAdapter(comm, olecon); DataSet ds = new DataSet(); odda.Fill(ds, "good"); dataGridView1.DataSource = ds.Tables["good"]; olecon.Close(); 如果是sql数据库的话把所有以OleDb开头都换成Sql,再修改下连接字符串就可以了我的是access数据库 谢谢各位!已经实现了, 修改,删除,增加。 代码大概如下: 还有一个问题,其中 dataGridView1.CancelEdit(); 在修改错误的时候,他没有起作用。 有谁了解的吗?删除的功能放哪里好点,我是放在RowHeaderMouseDoubleClick的事件里面了,有什么比较好的解决方式吗,不想单独在外面放一个按钮。 string ConnectionString= "***********"; string TbName = null; DataSet Ds = null; SqlConnection Con = new SqlConnection(ConnectionString); SqlCommand Cmd = new SqlCommand(); SqlDataAdapter Sda = new SqlDataAdapter(); private void GridViewBind() { try { string str = "SELECT * FROM " + TbName ; Ds = new DataSet(); Sda.SelectCommand = new SqlCommand(str , Con); Sda.Fill(Ds, tbName); dataGridView1.DataSource = Ds;//Ds.Tables[tbName] dataGridView1.DataMember = tbName; //dataGridView1.Columns[0].ReadOnly = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { SqlCommandBuilder SCB = new SqlCommandBuilder(Sda); return Sda.Update(Ds, tbName); } catch (Exception ex) { MessageBox.Show(ex.ToString()); dataGridView1.CancelEdit(); } } private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { if (MessageBox.Show("\n确定要删除你选择的这行数据吗?\n", "删除行", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index); SqlCommandBuilder SCB = new SqlCommandBuilder(Sda); return Sda.Update(Ds, tbName); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } 上面删除那块 return 得去了。 return Sda.Update(Ds, tbName); 其实就2句:SqlCommandBuilder SCB = new SqlCommandBuilder(Sda);Sda.Update(Ds, tbName);一开始一直出错,我还以为是语句的问题。 “对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。”这个错误其实就是你的表没有设主键,设上就可以更新了。 ok散分。 问个内存分配的问题~多谢 代码中判断ms sql server数据库备份文件的有效性 c#基础问题,请求帮忙! ComboBox里面不同的索引项的怎么绑定不同的数字编码 加入"组播(多播)"需要等待时间吗? 求教:想用 SourceSafe 作 VS2003 版本管理工具,用拿一个版本的SourceSafe,从哪获得?谢谢!! 兄弟我建了一个qq群,8640323,欢迎各位兄弟加入讨论 要放假了,要放假了! 请问c#里面的属性是干什么用的, 如何用C#实现在线升级的功能 同步操作 关于遍历子菜单的问题!
要是不好你也别怪我!
至于删除某行:if (this.DataGridView1.CurrentRow != null)
{
if (!DataGridView1.Rows[this.DataGridView1.CurrentRow.Index].IsNewRow)
{
this.DataGridView1.Rows.Remove(DataGridView1.Rows[this.DataGridView1.CurrentRow.Index]);
BigMessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
} }添加和修改某行信息:
直接在下列事件中处理就行了,保存时把DataGridView1里的信息保存在起来就好了private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{}
http://windowsclient.net/downloads/folders/applications/default.aspx
olecon.Open();
string comm = "select * from [jingyou]";
OleDbDataAdapter odda = new OleDbDataAdapter(comm, olecon);
DataSet ds = new DataSet();
odda.Fill(ds, "good");
dataGridView1.DataSource = ds.Tables["good"];
olecon.Close();
我的是access数据库
代码大概如下:
还有一个问题,其中 dataGridView1.CancelEdit(); 在修改错误的时候,他没有起作用。 有谁了解的吗?
删除的功能放哪里好点,我是放在RowHeaderMouseDoubleClick的事件里面了,有什么比较好的解决方式吗,不想单独在外面放一个按钮。 string ConnectionString= "***********";
string TbName = null; DataSet Ds = null;
SqlConnection Con = new SqlConnection(ConnectionString);
SqlCommand Cmd = new SqlCommand();
SqlDataAdapter Sda = new SqlDataAdapter(); private void GridViewBind()
{
try
{
string str = "SELECT * FROM " + TbName ;
Ds = new DataSet();
Sda.SelectCommand = new SqlCommand(str , Con);
Sda.Fill(Ds, tbName); dataGridView1.DataSource = Ds;//Ds.Tables[tbName]
dataGridView1.DataMember = tbName;
//dataGridView1.Columns[0].ReadOnly = true; }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{ SqlCommandBuilder SCB = new SqlCommandBuilder(Sda);
return Sda.Update(Ds, tbName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
dataGridView1.CancelEdit();
}
} private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (MessageBox.Show("\n确定要删除你选择的这行数据吗?\n", "删除行", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
SqlCommandBuilder SCB = new SqlCommandBuilder(Sda);
return Sda.Update(Ds, tbName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
SqlCommandBuilder SCB = new SqlCommandBuilder(Sda);
Sda.Update(Ds, tbName);一开始一直出错,我还以为是语句的问题。
“对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。”
这个错误其实就是你的表没有设主键,设上就可以更新了。 ok散分。