怎样取出DataGrid的数据字段的内容呀?

CSDNAsk 2004-11-30 10:59:48
怎样取出DataGrid的数据字段的内容呀?
...全文
196 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
linlinunix 2004-12-02
  • 打赏
  • 举报
回复
CurrentColumnIndex
linlinunix 2004-12-02
  • 打赏
  • 举报
回复
利用DataGridTableStyle 比较麻烦这样吧本法自但是肯定行
DataTable dt = (DataTable)DataGrid.DataSource
列名称 = dt.Columns[DataGrid.CurrentCell.CurrentRowIndex].Caption
skyeenet 2004-12-02
  • 打赏
  • 举报
回复
DataGrid.cell(....).toString
就可以了
linlinunix 2004-12-02
  • 打赏
  • 举报
回复
利用DataGridTableStyle 比较麻烦这样吧本法自但是肯定行
DataTable dt = (DataTable)DataGrid.DataSource
列名称 = dt.Columns[0].Caption
lr2651 2004-12-02
  • 打赏
  • 举报
回复
DataGridTableStyle是什么?要引用什么呀?

这个你加如WebControl命名空间再声明DataGrid后就有了
shj777 2004-12-02
  • 打赏
  • 举报
回复
楼主总是说“再次声明,不是数据,是每一列的数据字段名”
上面的各位仁兄给你的解决办法求的就是数据字段名啊!
能不能不总是声明、声明的。
nga96 2004-12-02
  • 打赏
  • 举报
回复
要不用DATAGRIDSTYLE,绑定到DATAGRID,再去取DATAGRIDSTYLE的列名就OK啦,呵我就这样干的
anantnt203120 2004-12-02
  • 打赏
  • 举报
回复
up 有分吗?
CSDNAsk 2004-12-02
  • 打赏
  • 举报
回复
再次声明,不是数据,是每一列的数据字段名。
CSDNAsk 2004-11-30
  • 打赏
  • 举报
回复
DataGridTableStyle是什么?要引用什么呀?
tfrtfr 2004-11-30
  • 打赏
  • 举报
回复
DataGridTableStyle ts = new DataGridTableStyle();
ts = dataGrid.TableStyles[0];
colName = ts.GridColumnStyles[i].MappingName;
CSDNAsk 2004-11-30
  • 打赏
  • 举报
回复
不是的,我的意思是取出那一列的字段名。
hxy51899 2004-11-30
  • 打赏
  • 举报
回复
DataGrid1.SelectedItem.Cells[字段所在的列].Text,这是取选中那行的列值。
cqandy 2004-11-30
  • 打赏
  • 举报
回复
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataSet dataSet1=new DataSet();
dataSet1=(DataSet)ViewState["myDst"];
ViewState["myDst"] = dataSet1;
int rowindex=e.Item.ItemIndex;
string updno=((TextBox)e.Item.Cells[2].Controls[0]).Text;//取DataGrid中某行中各cell的值
string updval1=((TextBox)e.Item.Cells[3].Controls[0]).Text;
string updval2=((TextBox)e.Item.Cells[4].Controls[0]).Text;
string updval3=((TextBox)e.Item.Cells[5].Controls[0]).Text;
string updval4=((TextBox)e.Item.Cells[6].Controls[0]).Text;
string updval5=((TextBox)e.Item.Cells[7].Controls[0]).Text;
string updval6=((TextBox)e.Item.Cells[8].Controls[0]).Text;
fhyin 2004-11-30
  • 打赏
  • 举报
回复
dataGrid[行号,列号]
marvelstack 2004-11-30
  • 打赏
  • 举报
回复
在程序中访问DataGrid中的内容
DataTable中有数据行DataRow,而在DataGrid中没有行这个对象,这让人感到很不习惯,
也觉得不够自然。在DataTable中,一张表的层次结构很清楚,
DataTable.Rows属性可以得到这张表所包含的所有行的行集,
通过行集的索引DataRowCollection[index]就可以得到具体的一个DataRow,
数据行的索引DataRow[index]又可以得到这一行的具体某一列的内容。
而DataGrid中就没有这么方便了,DataGrid只有两个属性可用,
DataGrid.CurrentCell 属性,此属性返回一个DataGridCell类型的结构,
DataGridCell结构指明此Cell所在的行号和列号。还有一个DataGrid.Item 属性,
此属性有两个重载: public object this[DataGridCell]
//获取或设置指定的 DataGridCell 的值 public object this[int, int]
//获取或设置位于指定行和列的单元格的值 11
12
可见,DataGrid中访问都是针对某个Cell进行的。
经常的,我们需要从当前的Cell获得此Cell所对应的DataRow,
比如界面中可能先选中DataGrid的某一行,或者某一个Cell,
然后点击一个按钮,弹出一个新的窗口,窗口中显示这一行的所有单元的内容,
并允许修改单元的值,最后保存关闭窗口。
这就需要从当前的DataGrid所在的单元找到其所对应的DataTable所在的行和列。
而DataGrid中显示的数据可能经过DataView的DataView.RowFilter属性、
DataView.RowStateFilter属性的过滤,还可能经过DataGrid本身根据各个列的正向和反向排序,
所以DataGrid的CurrentRowIndex属性所指示的行索引跟其对应的DataTable的行索引有很大的机会
是不一样的,不能够根据DataGrid的CurrentRowIndex去获取其对应的DataTable的行。
这时BindingManagerBase又将发挥作用了,
我们可以先建立一个对应此DataGrid绑定的数据源的BindingManagerBase,
这样这个BindingManagerBase就可以管理这个数据源。
//设置DataGrid的数据源
dataGrid1.DataSource = myDataSet; dataGrid1.DataMember = "customers";
//建立同DataGrid同样数据源的BindingManagerBase
BindingManagerBase myBindingManagerBaseParent = this.BindingContext[myDataSet,"customers"];
一旦建立了这个BindingManagerBase,
就可以通过BindingManagerBase的当前行的属性来获取当前数据源的记录:
//BindingManagerBase的Current返回数据源的对象,对于绑定到DataView的数据源,需要将此对象显式
//的转换为 DataRowView类型
DataRowView myDataRowView =(DataRowView) myBindingManagerBaseParent.Current
这样,我们就可以从当前的Cell得到此Cell所在的DataRowView,
DataRowView又可以通过DataRowView.Row属性及其方便的得到DataRow。
如果还要进一步,想要得到此Cell所对应的DataTable的具体单元,
就是不光要得到DataRow,还要知道这个Cell所对应的列。
这又分两种情况: 一是DataGrid未使用TableStyles来设置DataGrid要显示的列和格式,
数据源DataView的所有列都将按照DataView本身的顺序显示出来,
这样可以直接取得对应的列索引:
//获取当前DataGrid单元的列索引,这个索引跟DataTable的索引是一样的
Int ColumnNumber = DataGrid.CurrentCell.ColumnNumber;
13
另一种情况是DataGrid使用了TableStyles来设置DataGrid要显示的列和格式,
这样DataGrid单元的列索引跟DataTable的索引就可能是不一样的了,
这就要用DataGrid的TableStyles了:
Int ColumnNumberDataGrid = DataGrid.CurrentCell.ColumnNumber;
//获取当前DataGrid单元的列索引
Int ColumnNumberDataTable = DataGrid.TableStyles[0].GridColumnStyles[ColumnNumberDataGrid].MappingName

110,536

社区成员

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

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

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