datagrid 列的显示值和数据值 不同

lilia 2003-09-15 11:38:31
如:“顾客”列 我想显示出“A公司”,而数值为“00001”
怎么办呢?
...全文
151 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hmjwdmh 2003-09-15
  • 打赏
  • 举报
回复
方法一样呀
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
string ss = e.Item.Cells[X].Text;
if (ss.Trim()=="00001")
{
e.Item.Cells[X].Text = "A公司";
}
}
lilia 2003-09-15
  • 打赏
  • 举报
回复
一般的数据库应用都有这个要求阿

界面上一般显示你的名字 如“张三”什么的
而数据库里保存的是你的 id ,如 身份证号什么的阿
lilia 2003-09-15
  • 打赏
  • 举报
回复
winform的datagrid
lilia 2003-09-15
  • 打赏
  • 举报
回复
我想让 datagrid 列的显示值和数据值 不同

如:“顾客”列 ,不能显示“00001” 给用户把?
我想显示出“A公司”,而数值为“00001”
hmjwdmh 2003-09-15
  • 打赏
  • 举报
回复
用DataGrid的 itemdatabound事件就可以
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{

string ss4 = e.Item.Cells[4].Text;
if (ss4.Substring(0,2)=="DF")
{
ss4 = ss4.Substring(5,4);
string ss5 = zfc(e.Item.Cells[7].Text.Trim());
e.Item.Cells[6].Text = zxgl(ss5,ss4);
}
}
wkyjob 2003-09-15
  • 打赏
  • 举报
回复
没办法,帮不了你!说清楚些。
wd_318 2003-09-15
  • 打赏
  • 举报
回复
可以参考一下
http://www.csdn.net/develop/read_article.asp?id=16086
skykevin 2003-09-15
  • 打赏
  • 举报
回复
把该列绑定到ComboBox上,方法如下:
把如下类放入你的名称空间
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"存放变换关系: 00001对应A公司,00002对应B公司,00003对应C公司
//npk对应:00001、00002、00003
//vcName对应:A公司,B公司,C公司

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

HenanBoy 2003-09-15
  • 打赏
  • 举报
回复
什么意思呀。你说清楚点好不好
lilia 2003-09-15
  • 打赏
  • 举报
回复
skykevin(天下) :
报错啊 。
正在学用。。。
skykevin 2003-09-15
  • 打赏
  • 举报
回复
耐心按楼上skykevin(天下)的方法做,就OK了。DataGrid没有现成的ComboBox,要自己做。相信我,没错的。
lilia 2003-09-15
  • 打赏
  • 举报
回复
提!
lilia 2003-09-15
  • 打赏
  • 举报
回复
无为:
是 winform 阿 :)

110,526

社区成员

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

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

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