DataGrid的数据帮定问题,只要代码帮定,不要控件帮定。

wjhs 2003-08-22 04:47:42
用代码写出怎用给DataGrid 提供数据。
另,Sql数据库数据更新,即:插入,修改,删除等的代码
如:insert into
delete
语句。
...全文
80 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
海淀网友 2003-08-30
  • 打赏
  • 举报
回复
看完了上面的各位老兄的教导,但没有一个能成,雪狼的有一个地方是不对的,就是net DataSet的写法好像不能过。


private void button1_Click(object sender, System.EventArgs e)
{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=zy-server;database=ztaifdata;uid=sa;password=zhiyou");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from sysobjects",conn);
DataSet dt = new DataSet();
da.Fill(dt,"sysobjects");
dataGrid1.SetDataBinding(dt,"sysobjects");

}
wjhs 2003-08-28
  • 打赏
  • 举报
回复
to Snof(雪狼)请问,你能不能写得清楚一些,我有点不太明白。谢谢啦!
greenhill1 2003-08-26
  • 打赏
  • 举报
回复
TextBoxDoubleClickHandler()和TextBoxDoubleClickHandler()两个事件合起来才是完整的双击事件,你可以分别只写其中的一个试试效果就知道为什么了
greenhill1 2003-08-26
  • 打赏
  • 举报
回复
那是datagrid的某单元格的双击事件,要先在datagrid的mousedown事件中获得点击的时间具体代码如下:
private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
gridMouseDownTime = DateTime.Now;
}
private void TextBoxDoubleClickHandler(object sender, EventArgs e)
{
neirong=((TextBox)sender).Text.ToString();
num=dataGrid1.CurrentCell.RowNumber;
kemu=dataGrid1[num,1].ToString().Trim();
homework myform=new homework();
myform.ShowDialog();
dataGrid1banding();
}
private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)
{
if(DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
{
neirong=((TextBox)sender).Text.ToString();
num=dataGrid1.CurrentCell.RowNumber;
kemu=dataGrid1[num,1].ToString().Trim();
homework myform=new homework();
myform.ShowDialog();
dataGrid1banding();
}
}
好运 2003-08-26
  • 打赏
  • 举报
回复
to greenhill1(小山) ,TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
怎样写的?
greenhill1 2003-08-23
  • 打赏
  • 举报
回复
private void dataGrid1banding()
{
//int numCols;
try
{
dataGrid1.CaptionText=toNew3+toNew4+DateTime.Today.ToString("M月d日")+"作业:";

string sqlSelect="SELECT ID,科目,内容 FROM 作业 WHERE 年级="+"'"+toNew3+"'"+"AND 班级="+"'"+toNew4+"'"+"AND 日期="+"'"+DateTime.Today+"'";

SqlConnection thisConnection=new SqlConnection(@"Data Source=SERVER;User ID=sa;Password=;Initial Catalog=dx");

thisConnection.Open();

SqlDataAdapter thisAdapter=new SqlDataAdapter(sqlSelect,thisConnection);

SqlCommandBuilder thisBuilder=new SqlCommandBuilder(thisAdapter);

DataSet thisDataSet=new DataSet();

thisAdapter.Fill(thisDataSet,"作业");

dataGrid1.DataSource = thisDataSet.Tables["作业"];

DataGridTableStyle dgts = new DataGridTableStyle();

dgts.MappingName = "作业";

/*DataGridTableStyle tableStyle = dataGrid1.TableStyles["作业"];

tableStyle.GridColumnStyles[0].Width =0;
tableStyle.GridColumnStyles[1].Width =60;
tableStyle.GridColumnStyles[2].Width =485;

tableStyle.DataGrid.Refresh();*/

DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "ID";
TextCol.HeaderText = "序号";
TextCol.Width = 0;

//TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
//TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
dgts.GridColumnStyles.Add(TextCol);

TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "科目";
TextCol.HeaderText = "科目";
TextCol.Width = 60;

//TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
//TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
dgts.GridColumnStyles.Add(TextCol);

TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "内容";
TextCol.HeaderText = "内容(双击某行内容可以进行该内容的修改)";
TextCol.Width = 485;

TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
dgts.GridColumnStyles.Add(TextCol);

//DataRow seleRow=new DataRow();

dataGrid1.TableStyles.Add(dgts);
thisConnection.Close();
}
catch
{
}
}
ArEoN 2003-08-22
  • 打赏
  • 举报
回复
数据插入,删除操作一样,都是执行Sql语句
feeling_vb 2003-08-22
  • 打赏
  • 举报
回复
第一、
给dataGrid提供数据

string strCon = @"uid=sa; pwd=; Initial Catalog=数据库名称; Data Source=数据源;";
SqlConnection connection = new SqlConnection ( );

connection.ConnectionString = strCon;

connection.open( );

string strSql = " select * from tabname ";

SqlCommand cmd = new SqlCommand ( strSql,connection );

SqlDataAdapter dataAdapter = new SqlDataAdapter ( cmd );

DataSet dt = new DataSet ( );

dataAdapter.Fill ( dt );

dataGrid.datasource = dt.table[0].defaultView;

dataGrid.DataBind( );

connection.close( );

数据插入修改等自己慢慢捉摸吧,我要踢球去了!!!

jeall 2003-08-22
  • 打赏
  • 举报
回复
gshope上面代码含义:
1、先显示数据信息
2、进行Insert操作

DataGrid中可以直接定义<删除><修改><添加>事件
<删除><修改><添加>均有相应的DataGrid事件DeleteCommand、UpdateCommand、InsertCommand对应。
xiangzionest 2003-08-22
  • 打赏
  • 举报
回复
1.邦定datagrid
sqlDataAdapter1.Fill (dataSet11,"table1");
DataGrid1.DataSource=dataSet11.Tables["table1"];
DataGrid1.DataBind();

2.
SqlConnection myconn=new SqlConnection(".....");
myconn.Open ();
string sql="insert....."//这里定义sql语句
SqlCommand mycommand=new SqlCommand(sql,myconn);
int iResult=mycommand.ExecuteNonQuery () ;
......
雪狼1234567 2003-08-22
  • 打赏
  • 举报
回复
比较完整的例子吧
首先取得数据,放到DataGrid里

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");

然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
如果需要,可以绑定TextBox来作录入,而用DataGrid显示
this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
最后把结果写回数据库:


// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{

return false;
}
finally
{
conn.Close();
}
caoit 2003-08-22
  • 打赏
  • 举报
回复
不太明白,
gshope 2003-08-22
  • 打赏
  • 举报
回复
1.
.....
sqlDataAdapter1.Fill (dataSet11);
DataGrid1.DataSource=dataSet11;
DataGrid1.DataBind();

2.
SqlConnection myconn=new SqlConnection(".....");
myconn.Open ();
sql="insert....."
SqlCommand mycommand=new SqlCommand(sql,myconn);
SqlDataReader myreader;
myreader=mycommand.ExecuteNonQuery () ;
......

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧