有关dataGrid绑定dataView的问题

JFlame 2004-11-21 01:37:24

有2个表,表A有三个字段 name_id ,address, tel ,表B有两个字段name,id .

表B中的id和表A中的name_id是对应的.

我想绑定dataGrid到表A,但是想让name_id的位置显示name,而且对表A的修改都可以在dataGrid中进行.
可以吗?
...全文
224 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ClampHammer 2005-01-05
  • 打赏
  • 举报
回复
// note: on the very first time this routine is called you MUST set the column as visible
// ahead of setting a position in the items collection. otherwise the combobox will not be
// populated and the call to set the SelectedValue cannot succeed

//if there is no this value in this combobox,Don't display the combobox
DataRow[] aRowA;
aRowA=((DataTable)_objSource).Select(_strValue + " = " + anObj);

if(aRowA.Length>0)
{
_cboColumn.Visible = true;
}
// use the object to set the combobox. the null detection is primarily aimed at the addition of a
// new row (where it is possible a default column-row content has not been defined)
if (anObj.GetType() != typeof(System.DBNull))
{
_cboColumn.SelectedValue = anObj;
}
else
{
_cboColumn.SelectedIndex = 0;
}
// we've set the combobox so we can now paint the control and move focus onto it
_cboColumn.EndUpdate();
_cboColumn.Focus();
// below is the default method which we definitely DONT want to call as the text box must remain dormant!
// base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);

//做呈现用,禁止编辑
_cboColumn.Visible=false;
}

public void cboColumn_Leave(object sender, EventArgs e)
{
// We are going to write back the ValueMember from combobox into the current column-row in the
// table displayed by the DataGrid control. Finally we hide the combobox. note the source and
// row were saved when the edit began - see Edit()
object objValue = _cboColumn.SelectedValue;
// we can write System.DBNull back to a database but we cannot write null (which would
// cause an exception). if the combobox is defined as a dropdownlist we cannot see the
// null value. However if the combobox is defined as a dropdown then editing into the
// combobox will, by default, generate a null value. For this possibility we translate
// null to the System.DBNull value

if (objValue == null)
{
objValue = DBNull.Value;
}
this.SetColumnValueAtRow(_cmSource, _iRowNum, objValue);
_cboColumn.Visible = false;
}

// this method is called to draw the box without a highlight (ie when the cell is in unselected state)
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{

// there are three Paint() methods that can be overriden. I put break points in all three, but this
// was the only one I caught. If you find odd paint behaviors please let me know and I'll investigate

string strCountry;
DataRow[] aRowA;

// retrieve the value at the current column-row within the source for this column
object anObj = this.GetColumnValueAtRow(source, rowNum);
// use this value to access the datasource. again, we must allow that a null object
// is returned; this typically only happens when adding a new row to the DataGrid host
Type aType = anObj.GetType();
if (aType != typeof(System.DBNull))
{
if(aType==typeof(System.String))
{
aRowA = ((DataTable)_objSource).Select(_strValue + "='" + anObj+"'");
}
else
{
aRowA = ((DataTable)_objSource).Select(_strValue + "=" + anObj);
}
if(aRowA.Length<=0)
{
strCountry="-无-";
}
else
{
strCountry = aRowA[0][_strMember].ToString();
}
}
else
{
strCountry=this.NullText;
}
//if (aRowA.Length > 0)
//{
// strCountry = aRowA[0][_strMember].ToString();
//}
// all we are going to do is repaint the cell. Empiric observation indicates this code is ONLY
// called when the column is not in edit mode, however you could wrap conditional code around
// this to block an unwanted paint event calling during an edit operation
Rectangle rect = bounds;
// use custom background color if the property was set by the User
if (this._backBrush == null)
g.FillRectangle(backBrush, rect);
else
g.FillRectangle(_backBrush, rect);
// vertical offset to account for frame of combobox
rect.Y += 2;
if (this._foreBrush == null)
g.DrawString(strCountry, this.TextBox.Font, foreBrush, rect);
else
g.DrawString(strCountry, this.TextBox.Font, _foreBrush, rect);
}

public System.Drawing.Color backgroundColour
{
set { if (value == System.Drawing.Color.Transparent) this._backBrush = null; else this._backBrush = new SolidBrush(value); }
}

public System.Drawing.Color foregroundColour
{
set { if (value == System.Drawing.Color.Transparent) this._foreBrush = null; else this._foreBrush = new SolidBrush(value); }
}

// below are the two other Paint() and the other Edit() override. if you re-enable the code and set a
// breakpoint in each, you can test to see if either method gets called!
/*
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
{
base.Paint(g, bounds, source, rowNum);
}

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
{
base.Paint(g, bounds, source, rowNum, alignToRight);
}

protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly)
{
base.Edit (source, rowNum, bounds, readOnly);
}
*/

private void TextBox_MouseMove(object sender, MouseEventArgs e)
{
//this.TextBox.Focus();
}
}
}
ClampHammer 2005-01-05
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace ss_manage
{
/// <summary>
/// This DataGrid Column class implements a ComboBox Column. The combo box is fed from a
/// table containing an collection of value objects and a string descriptor (the displayed
/// element). On losing focus, the combo box the current cell in the associated DataGrid
/// is updated with the current selected value object
/// </summary>
public class MyComboColumn : System.Windows.Forms.DataGridTextBoxColumn
{
// each column shares a single combobox
public ComboBox _cboColumn;
// data we save when the column is instantiated (no provision for subsequent rebinds)
private object _objSource;
private string _strMember;
private string _strValue;
// remember if we have bound the combobox to the parent datagrid control
private bool _bIsComboBound = false;
// data that describes the background and foreground colors used to paint the cell when not in edit mode
private Brush _backBrush = null;
private Brush _foreBrush = null;

// information picked up and held when we start to edit the source table
private int _iRowNum;
private CurrencyManager _cmSource;

/// <summary>
/// initialize the combobox column and take note of the data source/member/value used to fill the combobox
/// </summary>
/// <param name="objSource">bind Source for the combobox (typical is a DataTable object)</param>
/// <param name="strMember">bind for the combobox DisplayMember (typical is a Column Name within the Source)</param>
/// <param name="strValue">bind for the combobox ValueMember (typical is a Column Name within the Source)</param>
public MyComboColumn(object objSource, string strMember, string strValue, bool bUseDropDownList)
{
_objSource = objSource;
_strMember = strMember;
_strValue = strValue;

// create a new combobox object
_cboColumn = new ComboBox();
// set the data link to the source, member and value displayed by this combobox
_cboColumn.DataSource = _objSource;
_cboColumn.DisplayMember = _strMember;
_cboColumn.ValueMember = _strValue;
if (bUseDropDownList == true)
{
// we cannot create new countries through this column so disallow editing by making the combo a drop-down list
_cboColumn.DropDownStyle = ComboBoxStyle.DropDownList;
// Setting ReadOnly changes the behavior of the column so the 'leave' event fires whenever we
// change cell. The default behavior will not fire the 'leave' event when we up-arrow or
// down-arrow to the next row.
this.ReadOnly = true;
}
else
{
// because this is not an edit-through combo we are going to suppress key-strokes.
// notice this routine does not affect navigation or delete keys. The delete key
// allows us to select the null data value for this row

_cboColumn.KeyPress += new KeyPressEventHandler(_cboColumn_KeyPress);
this._cboColumn.DropDownStyle=ComboBoxStyle.Simple;
this.ReadOnly=true;
}
// we need to know when the combo box is getting closed so we can update the source data and
// hide the combobox control
_cboColumn.Leave += new EventHandler(cboColumn_Leave);
// make sure the combobox is invisible until we've set its correct position and dimensions
_cboColumn.Visible = false;
this.TextBox.MouseMove+= new MouseEventHandler(TextBox_MouseMove);
}



private void _cboColumn_KeyPress(object sender, KeyPressEventArgs e)
{
// mark all key events as handled to block editing of combobox entries
e.Handled = true;
}

protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
// the navigation path to the datagrid only exists after the column is added to the Styles
if (_bIsComboBound == false)
{
_bIsComboBound = true;
// important step here if we want to properly handle key events! the next step cannot
// be performed until the object is bound to the DataGrid (or an Exception must occur)
this.DataGridTableStyle.DataGrid.Controls.Add(_cboColumn);
}
// this data is used when the combo box loses focus
_iRowNum = rowNum;
_cmSource = source;
// synchronize the font size to the text box
_cboColumn.Font = this.TextBox.Font;
// we need to retrieve the current value and use this to set the combo box ahead of displaying it
object anObj = this.GetColumnValueAtRow(source, rowNum);

// set the combobox to the dimensions of the cell (do this each time because the user may have resized this column)
_cboColumn.Bounds = bounds;
// do not paint the control until we've set the correct position in the items list
_cboColumn.BeginUpdate();
ClampHammer 2005-01-05
  • 打赏
  • 举报
回复
对这个问题,有两种解决办法:
1.表连接。再填充DataSet,这样再以后每次增加,修改,删除数据时,都要重新Fill DataSet,数据量大时肯定很慢,我一般都是用第二种方法。
2.是我经常使用的办法,我就是用的DataView做数据源,就是把combobox内嵌DataGrid中,只让它起显示作用,把它的数据源设成表B,表A作为DataGrid的数据源,将name列设成含有下拉框的列。

注意:要使用自定义列样式。
zhiri 2005-01-05
  • 打赏
  • 举报
回复
可以直接写SQL语句,也可以用视图呀,这个问题是很简单的,我想你应该明白的。
JFlame 2005-01-05
  • 打赏
  • 举报
回复
up
JFlame 2004-11-22
  • 打赏
  • 举报
回复
还是不明白.
tonybaobao 2004-11-21
  • 打赏
  • 举报
回复
既然你涉及2张表,那么你应该在DB里面做,返回DataSet。
在DataGrid里面改当然可以,有EditCommand,UpdateCommand等等。
CSDNATM 2004-11-21
  • 打赏
  • 举报
回复
用SQL语句返回DATASET就行了吧
JFlame 2004-11-21
  • 打赏
  • 举报
回复
请详细说一下.
du9232 2004-11-21
  • 打赏
  • 举报
回复
建一个强数据类型的dataset,包含a表字段,和b表name字段
JFlame 2004-11-21
  • 打赏
  • 举报
回复
好麻烦啊.有没有简单点的方法.
loneghost 2004-11-21
  • 打赏
  • 举报
回复
数据库处理吧,
做视图,例如视图名为 UserVW,包含 name_id ,address, tel ,Name四个字段,name字段用关联id取得
绑定datagrid时候用UserVW绑定,修改得时候再调用修改表A,就可以了
JFlame 2004-11-21
  • 打赏
  • 举报
回复
自己顶.
第1章 空 第2章 ASP.NET运行模型 21.如何在页面中应用javascript脚本-示例1 21.如何在页面中应用javascript脚本-示例2 22.如何实现从服务器端向页面动态添加javascript脚本-示例1 22.如何实现从服务器端向页面动态添加javascript脚本-示例2 24.如何处理多页面重定向到同一页面后的返回问题 25.如何用Response.Redirect方法传递汉字 29.如何利用输出缓存技术缓存整个页面 30.如何利用片段缓存技术对用户控件进行缓存 31.如何利用数据缓存技术提高程序的性能 33.如何实现当页面产生错误时重定向到自定义错误界面 35.如何在程序中读写Web.config文件 37.如何使用CSS文件定义控件的样式 38.如何启用和禁用ViewState保存状态信息的功能 39.如何应用IsPostBack控制页面的加载 41.如何使用Trace对象进行跟踪调试(页面级) 42.如何使用#Include语法将文件添加到页面 43.如何使用编程的方式处理异常信息 44.如何将网页错误信息写入事件日志 第3章 常用Web服务控件 46.如何使用Label控件动态显示文本信息 47.如何实现当鼠标移到控件时显示提示信息 48.如何在Web应用程序中实现快捷键功能(使用脚本实现) 48.如何在Web应用程序中实现快捷键功能(使用属性实现) 49.如何设置页面上控件的Tab键顺序 50.如何实现用回车键代替Tab键的功能 51.如何为按钮键添加消息框 52.Button、LinkButton和ImageButton服务控件的区别及应用 54.如何使用CheckBox和CheckBoxList控件(CheckBox示例) 54.如何使用CheckBox和CheckBoxList控件(CheckBoxList示例) 55.如何使用RadioButton和RadioButtonList控件(RadioButton示例) 55.如何使用RadioButton和RadioButtonList控件RadioButtonList示例) 56.如何使用Panel控件操作一组控件 57.如何使用Table控件组织页面的内容 58.如何实现DropDownList控件选项的添加、删除等操作 59.如何实现间接改变DropDownList控件的当前选项 60.如何实现两个或多个DropDownList控件的联动 61.如何实现ListBox控件选项的上移、下移、添加和删除操作 62.如何实现ListBox控件中选项的双击事件 63.如何动态设置ListBox控件中各选项的背景颜色 64.如何在页面中动态创建控件 65.如何为HTML服务器控件的事件添加自定义方法 第4章 验证控件 67.如何使用RequiredFieldValidator控件验证用户是否输入信息 68.如何使用RegularExpressionValidator控件验证E-mail地址和邮编等格式 69.如何使用RangeValidator控件验证输入的信息是否在指定范围内 70.如何使用CompareValidator控件对两个输入控件比较验证 71.如何使用CustomValidator控件自定义验证格式 72.如何使用ValidationSummary控件总结所有验证控件的错误信息 73.如何创建自定义功能的验证控件 74.如何禁用服务器控件的验证功能 75.如何控制页面部分验证控件有效 76.如何实现控件即时验证功能 77.如何使用验证控件综合验证用户注册页面 78.如何验证DataGrid控件的编辑列 81.如何使用验证控件验证日期类型的输入(CompareValidator) 81.如何使用验证控件验证日期类型的输入(CustomerValidator) 81.如何使用验证控件验证日期类型的输入(RegularValidator) 第5章 高级Web控件 82.如何在网页中使用Calendar Web控件显示和选择日期 83.如何在Calendar Web控件中控制个别日的内容及显示 84.如何在Calendar Web控件中控制用户日期选定 86.如何使用XML控件显示XML文件和进行XSLT转换 87.如何使用AdRotator Web控件制作广告发布程序-示例1 87.如何使用AdRotator Web控件制作广告发布程序-示例2 88.如何创建用户控件 89.如何将Web窗体转换成用户控件 90.如何实现动态加载用户控件 92.如何使用TabStrip Web和MultiP
第2章 ASP.NET运行模型
21.如何在页面中应用javascript脚本-示例1
21.如何在页面中应用javascript脚本-示例2
22.如何实现从服务器端向页面动态添加javascript脚本-示例1
22.如何实现从服务器端向页面动态添加javascript脚本-示例2
24.如何处理多页面重定向到同一页面后的返回问题
25.如何用Response.Redirect方法传递汉字
29.如何利用输出缓存技术缓存整个页面
30.如何利用片段缓存技术对用户控件进行缓存
31.如何利用数据缓存技术提高程序的性能
33.如何实现当页面产生错误时重定向到自定义错误界面
35.如何在程序中读写Web.config文件
37.如何使用CSS文件定义控件的样式
38.如何启用和禁用ViewState保存状态信息的功能
39.如何应用IsPostBack控制页面的加载
41.如何使用Trace对象进行跟踪调试(页面级)
42.如何使用#Include语法将文件添加到页面
43.如何使用编程的方式处理异常信息
44.如何将网页错误信息写入事件日志
第3章 常用Web服务控件
46.如何使用Label控件动态显示文本信息
47.如何实现当鼠标移到控件时显示提示信息
48.如何在Web应用程序中实现快捷键功能(使用脚本实现)
48.如何在Web应用程序中实现快捷键功能(使用属性实现)
49.如何设置页面上控件的Tab键顺序
50.如何实现用回车键代替Tab键的功能
51.如何为按钮键添加消息框
52.Button、LinkButton和ImageButton服务控件的区别及应用
54.如何使用CheckBox和CheckBoxList控件(CheckBox示例)
54.如何使用CheckBox和CheckBoxList控件(CheckBoxList示例)
55.如何使用RadioButton和RadioButtonList控件(RadioButton示例)
55.如何使用RadioButton和RadioButtonList控件RadioButtonList示例)
56.如何使用Panel控件操作一组控件
57.如何使用Table控件组织页面的内容
58.如何实现DropDownList控件选项的添加、删除等操作
59.如何实现间接改变DropDownList控件的当前选项
60.如何实现两个或多个DropDownList控件的联动
61.如何实现ListBox控件选项的上移、下移、添加和删除操作
62.如何实现ListBox控件中选项的双击事件
63.如何动态设置ListBox控件中各选项的背景颜色
64.如何在页面中动态创建控件
65.如何为HTML服务器控件的事件添加自定义方法
第4章 验证控件
67.如何使用RequiredFieldValidator控件验证用户是否输入信息
68.如何使用RegularExpressionValidator控件验证E-mail地址和邮编等格式
69.如何使用RangeValidator控件验证输入的信息是否在指定范围内
70.如何使用CompareValidator控件对两个输入控件比较验证
71.如何使用CustomValidator控件自定义验证格式
72.如何使用ValidationSummary控件总结所有验证控件的错误信息
73.如何创建自定义功能的验证控件
74.如何禁用服务器控件的验证功能
75.如何控制页面部分验证控件有效
76.如何实现控件即时验证功能
77.如何使用验证控件综合验证用户注册页面
78.如何验证DataGrid控件的编辑列
81.如何使用验证控件验证日期类型的输入(CompareValidator)
81.如何使用验证控件验证日期类型的输入(CustomerValidator)
81.如何使用验证控件验证日期类型的输入(RegularValidator)
第5章 高级Web控件
82.如何在网页中使用Calendar Web控件显示和选择日期
83.如何在Calendar Web控件中控制个别日的内容及显示
84.如何在Calendar Web控件中控制用户日期选定
86.如何使用XML控件显示XML文件和进行XSLT转换
87.如何使用AdRotator Web控件制作广告发布程序-示例1
87.如何使用AdRotator Web控件制作广告发布程序-示例2
88.如何创建用户控件
89.如何将Web窗体转换成用户控件
90.如何实现动态加载用户控件
92.如何使用TabStrip Web和MultiPage Web控件制作选项卡式页面
93.如何使用Toolbar Web控件制作工具条式页面
94.如何识别Toolbar Web控件中哪个按钮激发的ButtonClick事件
95.如何使用TreeView Web控件
96.如何在TreeView Web控件中动态添加、删除TreeNode节点
100.如何使用TreeView Web控件中的TreeNodeType进行外观控制
101.如何使用TreeView Web控件绑定XML文件-示例1
101.如何使用TreeView Web控件绑定XML文件-示例2
第6章 数据服务控件
103.如何使用Repeater控件显示数据
104.如何使用Repeater控件的模板
105.如何实现Repeater控件的分页
106.如何使用DataList控件显示数据
107.如何实现选择DataList控件中的条目后显示该条目的详细信息
108.如何实现DataList控件的编辑功能
109.如何对DataList控件使用样式
110.如何在DataList控件中创建多个列
111.如何实现DataList控件的分页功能
112.如何在DataGrid中添加自动编号的功能
113.如何控制DataGrid绑定绑定的数据类型为日期的显示格式
114.如何把DataGrid中某一列的统计值显示在页脚中
115.如何在DataGrid中让不同条件的行显示不同的背景颜色
116.如何在DataGrid中实现分页功能
117.如何实现鼠标移到DataGrid控件的某一行时改变该行的背景
118.如何在DataGrid控件中实现自定义分页功能
119.如何在DataGrid中弹出一个详细信息窗口-示例1
119.如何在DataGrid中弹出一个详细信息窗口-示例2
120. 如何在DataGrid控件中添加CheckBox控件列
121.如何为DataGrid控件中的删除列添加确认框
122.如何使用DataGrid控件实现主细表
123.如何实现DataGrid控件的双向排序
124.如何动态的显示·隐藏DataGrid控件的某一列
125.如何实现DataGrid控件中DropDownList控件的绑定
126.如何在DataGrid控件中实现数据项的编辑、更新、取消
127.如何在DataGrid控件中删除最后一页的最后一记录
128.如何把数据库中的数据以Excel文件的格式在显示浏览器中
129.如何给DataGrid控件添加个标题并实现分类显示
130.如何实现DataGrid控件中DropDownList控件的联动
131.DataGrid控件使用综合举例
第7章 数据绑定技术
132.如何单值绑定到控件的属性
133.如何将DataTable绑定DataGrid Web控件
134.如何将DataSet绑定DataGrid Web控件
135.如何将DataView绑定DataGrid Web控件
136.如何将DataReader绑定DataGrid Web控件
137.如何将表中的列绑定到DropDownList Web控件
138.如何使用DataBinder.Eval()方法进行数据绑定
139.如何将ArrayList绑定到ListBox Web控件
140.如何将Hashtable绑定到RadioButtonList Web控件
141.如何将XML作为数据源绑定到控件
第8章 数据库设计
143.ASP.NET应用程序如何实现与SQL Server数据库的连接
144.ASP.NET应用程序如何实现与ACCESS数据库的连接
146.如何使用ADO.NET在数据库执行SQL语句
147.如何使用DataReader快速访问SQL Server数据
148.如何使用DataAdapter将数据填充到DataSet并显示出来
149.如何使用DataTable对象存储数据库表
150.如何对DataTable进行检索和排序
151.如何使用DataView进行数据排序和检索
152.如何在DataSet的DataTable间建立父子表关系
154.如何实现SQL Server数据库操作中的异常捕捉和处理
155.如何将数据库中的数据填充到XML文件中
157.如何使用Web.config配置数据库连接字符串
161.如何编写访问数据库的通用代码
162.如何在SQL Server数据库中编程获取用户表的数目和名称
163.如何获取SQL Server服务器端所有数据库列表
164.如何保存图片文件到SQL Server数据库
165.如何将数据库中保存的图片显示到页面中
166.如何在插入记录后获得记录的标识号
167.如何解决ADO.NET访问ACCESS数据库出现“操作必须使用一个可更新的查询”的问题
168.如何从EXCEL文件中读取数据
169.如何备份和恢复数据库
第9章 ASP.NET安全策略
第10章 常用功能及函数集
180.如何在ASP.NET中获得客户端IP地址
181.如何取得一定范围内的随机数
182.如何取得文件的扩展名示例一
182.如何取得文件的扩展名示例二
183.如何把字符串型数据转换为整型
184.如何取得当前系统时间
185.如何取得指定的年份中有多少天
186.如何取得客户端的鼠标坐标并反馈到服务器端
187.如何使用JMAIL组件实现邮件的发送
187.如何使用JMAIL组件实现邮件的接收
188.如何使用ASP.NET中的类库实现邮件的发送
189.如何生成图片的缩略图
190.如何在ASP.NET中动态的创建柱状和饼状统计图
192.如何判断上传图片的高度和宽度
193.如何使用DESCryptoServiceProvider类对数据或者文件进行加密解密
194.如何统计在线人数
195.如何在ASP.NET实现验证码
196.如何在本地取得指定网页的源代码
197.如何在ASP.NET中做一个日期选择器
198.如何在弹出对话框的同时保持页面的显示
199.如何点击按钮弹出新页面,输入数据后返回并且不刷新页面
第11章 文件操作
200.如何读取、修改文本文件
201.如何在ASP.NET中实现文件的上传
202.如何在ASP.NET中实现多文件的上传
203. 如何取得指定目录下的文件列表
204.如何在ASP.NET中创建日志文件
205.如何取得系统日志,并把日志信息写到文本文件中
206.如何在ASP.NET中创建、删除、复制文件
207.如何动态的创建HTML文件
第12章 XML相关处理技术

110,536

社区成员

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

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

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