請教大家用datagrid輸入數據和修改的思路?

GmLibra 2003-09-12 02:14:03
現在我做一張入倉單,要用一個datagrid來輸入數據,要有多條記錄。
所以我用datagrid 梆定了一個表(table1),datagrid重寫了DataGridColumnStyle的方法,添加了下拉框控件。
現在我能保存。但要修改它,就有問題。
我從數據庫查找出來這張單,寫入dataset(table2) 我顯示在datagrid上,我用dataGrid.SetDataBinding (dataset,"Table2");但這時下拉框控件就不能用。要重新棄置datagrid,很不方便。
且當我新增時又要用dataGrid.SetDataBinding (dataset,"Table1")來輸入數據。就會出現梆定錯誤。
我想把查找出來的Table2復制到Table1上。又復制不了,用dataset.Tables["Table1"] = dataset.Tables["Table2"].Copy()系統又說dataset.Tables["Table1"]只讀。點做,給我個思路吧。
請教:平時如何對datagrid這輸入修改保存進行操作。
...全文
49 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
skykevin 2003-09-12
  • 打赏
  • 举报
回复
如下方法做的下拉框控件不会出现你说的问题:


把如下类放入你的名称空间
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Diagnostics;

namespace 你的名称空间
{
// Derive class from DataGridTextBoxColumn
public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
// Hosted ComboBox control
private ComboBox comboBox;
private CurrencyManager cm;
private int iCurrentRow;

// Constructor - create combobox, register selection change event handler,
// register lose focus event handler
public DataGridComboBoxColumn()
{
this.cm = null;

// Create ComboBox and force DropDownList style
this.comboBox = new ComboBox();
this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;

// Add event handler for notification of when ComboBox loses focus
this.comboBox.Leave += new EventHandler(comboBox_Leave);
}

// Property to provide access to ComboBox
public ComboBox ComboBox
{
get { return comboBox; }
}

// On edit, add scroll event handler, and display combo box
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
Debug.WriteLine(String.Format("Edit {0}", rowNum));
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);

if (!readOnly && cellIsVisible)
{
// Save current row in the datagrid and currency manager associated with
// the data source for the datagrid
this.iCurrentRow = rowNum;
this.cm = source;

// Add event handler for datagrid scroll notification
this.DataGridTableStyle.DataGrid.Scroll += new EventHandler(DataGrid_Scroll);

// Site the combo box control within the bounds of the current cell
this.comboBox.Parent = this.TextBox.Parent;
Rectangle rect = this.DataGridTableStyle.DataGrid.GetCurrentCellBounds();
this.comboBox.Location = rect.Location;
this.comboBox.Size = new Size(this.TextBox.Size.Width, this.comboBox.Size.Height);

// Set combo box selection to given text
this.comboBox.SelectedIndex = this.comboBox.FindStringExact(this.TextBox.Text);

// Make the ComboBox visible and place on top text box control
this.comboBox.Show();
this.comboBox.BringToFront();
this.comboBox.Focus();
}
}

// Given a row, get the value member associated with a row. Use the value
// member to find the associated display member by iterating over bound datasource
protected override object GetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum)
{
Debug.WriteLine(String.Format("GetColumnValueAtRow {0}", rowNum));
// Given a row number in the datagrid, get the display member
object obj = base.GetColumnValueAtRow(source, rowNum);

// Iterate through the datasource bound to the ColumnComboBox
// Don't confuse this datasource with the datasource of the associated datagrid
CurrencyManager cm = (CurrencyManager)
(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);
// Assumes the associated DataGrid is bound to a DataView, or DataTable that
// implements a default DataView
DataView dataview = ((DataView)cm.List);

int i;

for (i = 0; i < dataview.Count; i++)
{
if (obj.Equals(dataview[i][this.comboBox.ValueMember]))
break;
}

if (i < dataview.Count)
return dataview[i][this.comboBox.DisplayMember];

return DBNull.Value;
}

// Given a row and a display member, iterating over bound datasource to find
// the associated value member. Set this value member.
protected override void SetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum, object value)
{
Debug.WriteLine(String.Format("SetColumnValueAtRow {0} {1}", rowNum, value));
object s = value;

// Iterate through the datasource bound to the ColumnComboBox
// Don't confuse this datasource with the datasource of the associated datagrid
CurrencyManager cm = (CurrencyManager)
(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);
// Assumes the associated DataGrid is bound to a DataView, or DataTable that
// implements a default DataView
DataView dataview = ((DataView)cm.List);
int i;

for (i = 0; i < dataview.Count; i++)
{
if (s.Equals(dataview[i][this.comboBox.DisplayMember]))
break;
}

// If set item was found return corresponding value, otherwise return DbNull.Value
if(i < dataview.Count)
s = dataview[i][this.comboBox.ValueMember];
else
s = DBNull.Value;

base.SetColumnValueAtRow(source, rowNum, s);
}

// On datagrid scroll, hide the combobox
private void DataGrid_Scroll(object sender, EventArgs e)
{
Debug.WriteLine("Scroll");
this.comboBox.Hide();
}

// On combo box losing focus, set the column value, hide the combo box,
// and unregister scroll event handler
private void comboBox_Leave(object sender, EventArgs e)
{
DataRowView rowView = (DataRowView) this.comboBox.SelectedItem;
string s = (string) rowView.Row[this.comboBox.DisplayMember];
Debug.WriteLine(String.Format("Leave: {0} {1}", this.comboBox.Text, s));

SetColumnValueAtRow(this.cm, this.iCurrentRow, s);
Invalidate();

this.comboBox.Hide();
this.DataGridTableStyle.DataGrid.Scroll -= new EventHandler(DataGrid_Scroll);
}
}
}


在你的含有DataGrid的FORM中(假定要变换的列为dataGridTextBoxColumn4)作如下改动:
//变量声明部分
private DataGridComboBoxColumn dataGridTextBoxColumn4;
//在private void InitializeComponent()中:
this.dataGridTextBoxColumn4 = new TySurvey.DataGridComboBoxColumn();


//创建一表"NameTable"存放变换关系: 0对应正常,1对应注销,2表示删除
//npk对应:0、1、2
//vcName对应:正常,注销,删除

//在Load事件中填加
dataGridTextBoxColumn4.ComboBox.DataSource = ds1.Tables["NameTable"];
dataGridTextBoxColumn4.ComboBox.DisplayMember = "vcName";
dataGridTextBoxColumn4.ComboBox.ValueMember = "nPK";


wkyjob 2003-09-12
  • 打赏
  • 举报
回复
看看也许对你有帮助:
#region 属性
public comboForm Frm
{
get {return _Frm;}
}
//我们将使用Index属性表示单元格内容与下拉列表的第Index列的内容相联系
public int Index
{
get {return _Index;}
set {_Index=value;}
}
#endregion

public DataGridComboFormColumn()
{
//
// TODO: 在此处添加构造函数逻辑
//
_Frm=new comboForm();
_ComboBox=new NoKeyUpComboBox();
_Frm.Deactivate+=new EventHandler(_Frm_deactive);
_Frm.DataGrid.Click+=new EventHandler(grid_click);
this._ComboBox.DropDown+=new EventHandler(_ComboBox_dropDown);
this._ComboBox.Leave+=new EventHandler(_ComboBox_leave);
}

//comboBox不具有焦点时隐藏
private void _ComboBox_leave(object sender,EventArgs e)
{
_ComboBox.Visible=false;
}

//下拉列表--Frm不具有焦点时隐藏
private void _Frm_deactive(object sender,EventArgs e)
{
_Frm.Hide();
_ComboBox.Visible=false;
}

//comboBox下拉时显示下拉列表--Frm
private void _ComboBox_dropDown(object sender,EventArgs e)
{
//在这里您还可以根据下拉列表的长与宽是否超出屏幕设置下拉列表的位置
_Frm.Left=_ComboBox.PointToScreen(new Point(0,_ComboBox.Height)).X;
_Frm.Top=_ComboBox.PointToScreen(new Point(0,_ComboBox.Height)).Y;
_Frm.Show();
_Frm.BringToFront();
}

//点击下拉列表的DataGrid时,将选中的内容写入单元格并隐藏下拉列表--Frm
private void grid_click(object sender,EventArgs e)
{
BindingManagerBase cm=_Frm.BindingContext[Frm.DataGrid.DataSource, _Frm.DataGrid.DataMember];
_ComboBox.Text=((DataRowView)cm.Current)[_Index].ToString();
this.TextBox.Text=((DataRowView)cm.Current)[_Index].ToString();
_Frm.Hide();
_ComboBox.Visible=false;
this.SetColumnValueAtRow(_source,rowNum,this.TextBox.Text);
}

//重载Edit方法,使用comboBox代替TextBox
protected override void Edit(CurrencyManager dataSource,int rowNum,Rectangle bounds,bool readOnly,string instanttext,bool cellVisible)
{
base.Edit(dataSource,rowNum,bounds,readOnly,instanttext,cellVisible);
_ComboBox.Parent=this.TextBox.Parent;
_ComboBox.Left=this.TextBox.Left-2;
_ComboBox.Top=this.TextBox.Top-2;
_ComboBox.Size=new Size(this.TextBox.Width,this._ComboBox.Height);
_ComboBox.Text=this.TextBox.Text;

this.TextBox.Visible=false;
_ComboBox.Visible=true;
_ComboBox.BringToFront();
_ComboBox.Focus();
_source=dataSource;
this.rowNum=rowNum;
}

}

110,526

社区成员

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

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

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