使用datagrid数据绑定来录入数据,请各位帮忙!急用,解决就马上给分……

Goldteerfei 2003-08-23 08:27:54
我现在使用datagrid数据绑定来录入数据,但是其中有写字段譬如ID类的需要是代码生成的,而不是由用户录入的,并且有写字段需要从列表框(组合框)中选择,这样的问题如何解决啊。
还有就是我录入某写字段的数据之后,在程序运行时能够看到这些记录,但是我把程序退出来再进去,就看不到那些记录了,可能那些记录没有存入到数据库中。
...全文
85 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪狼1234567 2003-08-25
  • 打赏
  • 举报
回复
增加,删除就如上面,最后要有一个保存按钮,
你按如下方法,项目->添加新项->数据窗体向导->按提示一步步来,
最后你看一个它自动生成的代码,已经实现你上面的功能啦
雪狼1234567 2003-08-25
  • 打赏
  • 举报
回复
增加,删除就如上面,最后要有一个保存按钮,
你按如下方法,项目->添加新项->数据窗体向导->按提示一步步来,
最后你看一个它自动生成的代码,已经实现你上面的功能啦
Goldteerfei 2003-08-25
  • 打赏
  • 举报
回复
DataGridComboBoxColumn 类在帮助中没有啊。
只有那个DataGridTextBoxColumn。
Goldteerfei 2003-08-25
  • 打赏
  • 举报
回复
to snof(雪狼) :
啊,我都晕了。还是没有搞懂啊。我想这个样子,有一个增加按钮,一个删除按钮,然后点增加按钮的话,在Datagrid中增加一行,然后每个字段顺序输入值。在这一行输完后,自动保存到数据库中。点击删除的时候,删除选中的当前行。
再就是datagrid中的字段不全,其中有写字段值需要用程序来确定。然后datagrid中可能有一列是需要从组合框中选择的,组合框的列表内容是“北京”“上海”“天津”。
我现在只想问我的增加和删除 按钮怎么写。
Goldteerfei 2003-08-25
  • 打赏
  • 举报
回复
to snof(雪狼) :
啊,我都晕了。还是没有搞懂啊。我想这个样子,有一个增加按钮,一个删除按钮,然后点增加按钮的话,在Datagrid中增加一行,然后每个字段顺序输入值。在这一行输完后,自动保存到数据库中。点击删除的时候,删除选中的当前行。
再就是datagrid中的字段不全,其中有写字段值需要用程序来确定。然后datagrid中可能有一列是需要从组合框中选择的,组合框的列表内容是“北京”“上海”“天津”。
我现在只想问我的增加和删除 按钮怎么写。
雪狼1234567 2003-08-25
  • 打赏
  • 举报
回复
1..那里的addnew指绑定的数据源新增,当然即然是绑定,那么datagrid及其它相关的也会显示新增的框框,
2..name,和stuno是你的数据库的表里的字段,它的意思是说,我有两个字段要更新,一个是stuno,一个是name,它们的值就是你在上面所说的数据源里新增,修改或删除的值,注意一点的是这儿增加的是参数,也就是说,上面sql语句里的@stuno,@name,
3..如果绑定的话,你不用取直接在DAtaGrid里新增一行的话,实际上在数据源DataSet.dataTable里已经有你新增的值啦,然后用sqlDataAdapter来UPdate就可以啦
4..DataGridComboBoxColumn 是系统定义的,你可以在帮助中找到
JCC0128 2003-08-25
  • 打赏
  • 举报
回复
前来学习
雪狼1234567 2003-08-25
  • 打赏
  • 举报
回复
1..
Goldteerfei 2003-08-25
  • 打赏
  • 举报
回复
to 雪狼:
还有DataGridComboBoxColumn 数据类型是从哪里来的。
Goldteerfei 2003-08-25
  • 打赏
  • 举报
回复
to 雪狼:
我还是不是很明白,首先
1、(增加):
this.BindingContext[dt,"student"].AddNew();
是指在哪里增加一行,其中dt 是datagrid ,但是“student”是表名吗?
2、(最后把结果写回数据库):
// 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"(这是什么)));

在什么时候把结果写回数据库。还有"stuno"和"name"这是什么,是输入的值吗?如果我不采用textbox 或者combobox 来输入的话,我怎么取datagrid列表中输入的值啊。

我看到你回复了很多关于datagrid 的问题,都是这样回复的。我想麻烦你根据我提问的问题,给我回复一下,好吗?

Goldteerfei 2003-08-25
  • 打赏
  • 举报
回复
我感觉很不好意思再麻烦你了。但是我还想问问那个DataGridComboBoxColumn 确实不是系统定义的啊,DataGridTextBoxColumn才是系统定义的。
DataGridComboBoxColumn 我就从帮助里没有找到啊。
zhangzs8896 2003-08-23
  • 打赏
  • 举报
回复
录入数据到数据库中,之后DataGrid绑定,这样就可以了
GmLibra 2003-08-23
  • 打赏
  • 举报
回复
查找一下,這個問題都有很多文章了,
雪狼1234567 2003-08-23
  • 打赏
  • 举报
回复
添加一个ComboBox列
Here are the notes from the code that list the 3 steps to add a combobox to your datagrid.

// Step 1. Derive a custom column style from DataGridTextBoxColumn

// a) add a ComboBox member

// b) track when the combobox has focus in Enter and Leave events

// c) override Edit to allow the ComboBox to replace the TextBox

// d) override Commit to save the changed data





// Step 2 - Use the combo column style

// Add 1 col with combo style

DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn();

ComboTextCol.MappingName = "custCity";

ComboTextCol.HeaderText = "Customer Address";

ComboTextCol.Width = 100;

ts1.GridColumnStyles.Add(ComboTextCol);



// Step 3 - Additional setup for Combo style

// a) make the row height a little larger to handle minimum combo height

ts1.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 3;

// b) Populate the combobox somehow. It is a normal combobox, so whatever...

ComboTextCol.ColumnComboBox.Items.Clear();

ComboTextCol.ColumnComboBox.Items.Add("Chicago");

ComboTextCol.ColumnComboBox.Items.Add("Corvallis");

ComboTextCol.ColumnComboBox.Items.Add("Denver");

ComboTextCol.ColumnComboBox.Items.Add("Great Falls");

ComboTextCol.ColumnComboBox.Items.Add("Kansas City");

ComboTextCol.ColumnComboBox.Items.Add("Los Angeles");

ComboTextCol.ColumnComboBox.Items.Add("Raleigh");

ComboTextCol.ColumnComboBox.Items.Add("Washington");



// c) set the dropdown style of the combo...

ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;


雪狼1234567 2003-08-23
  • 打赏
  • 举报
回复
DataGrid中下拉列表框
1.订阅dataGrid的单元格得到当标的事件

2.建立新的控件并绑定
private void dgdFunctionArea_GotFocus(object o, EventArgs e)
{
//Create the combo control to be added and set its properties
comboControl = new ComboBox();
comboControl.Cursor = System.Windows.Forms.Cursors.Arrow;
comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
comboControl.Dock = DockStyle.Fill;
comboControl.Items.AddRange(new string[5]{"","Information Technology","Computer Science","Bio Technology","Electrical Engg"});

//Create the date time picker control to be added and set its properties
DateTimePicker dtp = new DateTimePicker();
dtp.Dock = DockStyle.Fill;
dtp.Cursor = Cursors.Arrow;

//Create the check box control to be added and set its properties
CheckBox chk = new CheckBox();
chk.Dock = DockStyle.Fill;
chk.Cursor = Cursors.Arrow;

//Create the radio button control to be added and set its properties
RadioButton rb = new RadioButton();
rb.Dock = DockStyle.Fill;
rb.Cursor = Cursors.Arrow;

//Add the controls to the respective columns in the data grid
for(int i = 0 ;i < dataTable.Rows.Count ; i++)
{
//if the data in the first column is date time, add a date time control to the grid
if(dataGrid1[i,0].ToString().Equals("DateTime") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(dtp);
comboControl.SendToBack();
chk.SendToBack();
rb.SendToBack();
dtp.BringToFront();
}
//if the data in the first column is combo box, add a combo box control to the grid
else if(dataGrid1[i,0].ToString().Equals("ComboBox") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(comboControl);
chk.SendToBack();
dtp.SendToBack();
rb.SendToBack();
comboControl.BringToFront();
}
//if the data in the first column is check box, add a check box control to the grid
else if(dataGrid1[i,0].ToString().Equals("CheckBox") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(chk);
comboControl.SendToBack();
dtp.SendToBack();
rb.SendToBack();
chk.BringToFront();
}
//if the data in the first column is radio button, add a radio button control to the grid
if(dataGrid1[i,0].ToString().Equals("Radio Button") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(rb);
comboControl.SendToBack();
chk.SendToBack();
dtp.SendToBack();
rb.BringToFront();
}
datagridtextBox.TextBox.BackColor = Color.White;
}
}
雪狼1234567 2003-08-23
  • 打赏
  • 举报
回复
1,绑定数据做操作如下:
首先取得数据,放到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();
}
Goldteerfei 2003-08-23
  • 打赏
  • 举报
回复
大家都来帮忙啊。

110,538

社区成员

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

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

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