DataGrid列标题

编程入门 行业动态 更新时间:2024-10-12 01:26:59
本文介绍了DataGrid列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好NG: 我有以下代码可以使用。 GetProdStrtList()返回一个名为psTable的 DataTable。 Grid控件成功绑定到此 表并正确显示。我想将列标题更改为 除了SQL列名称之外的其他内容。例如,我更喜欢 " Component"到默认的ps_component。我还想自己设置 列宽属性。如您所见,我可以通过分配组件来设置网格的标题 。到.CaptionText属性。我没有 能够设置列标题属性。提前致谢。 干杯, Bob - Robert Schuldenfrei SI Inc. 32 Ridley Road Dedham,MA 02026 bo*@si-inc 781 / 329-4828 //获取并显示组件在网格中 DataTable psTable = ProdStrtTbl.GetProdStrtList(txtPartNo.Text); grdComponents.DataSource = psTable; grdComponents.CaptionText =" Components"; public static DataTable GetProdStrtList(string parentPart) { string selectStmt =" SELECT ps_parent," +" ps_component," +" ps_qty_per," +" ps_effectivity," +" ps_add_delete," +" ps_ref" +" FROM ProdStrt" +" WHERE ps_parent = @ps_parent ORDER BY ps_component" ; SqlConnection mcs3Connection = MCS3_DB.GetConnection(); SqlCommand selectCmd = new SqlCommand(selectStmt,mcs3Connection); selectCmd.Parameters.Add(" @ ps_parent",parentPart); SqlDataAdapter psDataAdapter = new SqlDataAdapter(selectCmd); DataSet psDataSet = new DataSet(); psDataAdapter.Fill(psDataSet," PS"); 返回psDataSet.Tables [" PS"]; }

解决方案

您有两个选择。您可以将相应列的ColumName设置为 ,无论您想要什么,如果您在绑定之前执行此操作,那么它将显示出来。 或者您可以创建datagridtablestyle ,以及datagridcolumnstyle并将其设置为 。 www.knowdotnet/articles/cgrid.html - WG Ryan MVP Windows - 嵌入式 对Microsoft Embedded的有效性发表意见新闻组? 让微软知道! https: //www.windowsembeddedeval/...ity/newsgroups " Robert Schuldenfrei" < bo*@s-i-inc>在消息中写道 news:vI_5d.167178

3l3.13131@attbi_s03 ...

你好NG: 我有以下代码可以使用。 GetProdStrtList()返回一个名为psTable的DataTable。 Grid控件已成功绑定到此表并正确显示。我想将列标题更改为SQL列名称之外的其他内容。例如,我更喜欢组件。到默认的ps_component。我还想自己设置列宽属性。如您所见,我可以通过分配组件来设置网格的标题。到.CaptionText属性。我还没有能够设置列标题属性。提前致谢。 干杯, Bob - Robert Schuldenfrei SI Inc. 32 Ridley Road Dedham,MA 02026 bo*@si-inc 781 / 329-4828 //获取并显示网格中的组件 DataTable psTable = ProdStrtTbl.GetProdStrtList(txtPartNo.Text); grdComponents.DataSource = psTable ; grdComponents.CaptionText =" Components"; public static DataTable GetProdStrtList(string parentPart) > string selectStmt =" SELECT ps_parent," +" ps_component," +" ps_qty_per," + " ps_effectivity," +" ps_add_delete," +" ps_ref" +" FROM ProdStrt" +" WHERE ps_parent = @ps_parent ORDER BY ps_component" ; SqlConnection mcs3Connection = M CS3_DB.GetConnection(); SqlCommand selectCmd = new SqlCommand(selectStmt,mcs3Connection); selectCmd.Parameters.Add(" @ ps_parent",parentPart); SqlDataAdapter psDataAdapter = new SqlDataAdapter(selectCmd); DataSet psDataSet = new DataSet(); psDataAdapter.Fill(psDataSet," PS") ; 返回psDataSet.Tables [" PS"]; }

感谢William的快速回复! 嗨Bob, 首先,我想确认一下我的理解你的问题。从 您的描述中,我了解到在将数据网格绑定到DataTable时需要更改数据网格表上显示的列名 。如果 有任何误解,请随时告诉我。 William给了我们一个使用tablestyle的好建议。这只是更改显示名称,但没有更改数据 源中的列名。此外,我们还可以使用ColumnMapping来实现这一目标。 在SqlDataAdapter.Fill之前,我们只需将DataTableMapping和一些 DataColumnMapping对象添加到TableMapping属性中 SqlDataAdapter。这将直接更改DataTable 中的DataColumn的名称。当您将DataGrid绑定到DataTable时,名称已经更改了 。 以下是一个示例, this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping [] { new System.Data.Common.DataTableMapping("表"," Employees",new System.Data.Common.DataColumnMapping [] { new System.Data.Common.DataColumnMapping(" EmployeeID"," ; IDofEmployee), 新的System.Data.Common.DataColumnMapping(LastName,姓氏), 新的System.Data.Common.DataColumnMapping( FirstName,FirstName), new System.Data.Common.DataColumnMapping(" Title"," Position")})}); 欲了解更多信息,请查看以下链接: msdn.microsoft/library/de...us/cpref/html/ frlrfsystemdatacommondataadapterclasstablemappings topic.asp HTH。 Kevin Yu ======= "此帖子已提供按现状 ;没有保证,也没有赋予 权利。

Hi NG: I have the following code working. The GetProdStrtList() returns a DataTable called psTable. A Grid control is successfully bound to this table and displays correctly. I would like to change the column headings to something other than the name of SQL column. For example I would prefer "Component" to the default "ps_component". I would also like to set the column width property myself. As you can see, I can set the grid''s caption by assigning "Components" to the .CaptionText property. I have not been able to set a column heading property. Thanks in advance. Cheers, Bob -- Robert Schuldenfrei S. I. Inc. 32 Ridley Road Dedham, MA 02026 bo*@s-i-inc 781/329-4828 //get and display the components in a grid DataTable psTable = ProdStrtTbl.GetProdStrtList(txtPartNo.Text); grdComponents.DataSource = psTable; grdComponents.CaptionText = "Components"; public static DataTable GetProdStrtList(string parentPart) { string selectStmt = "SELECT ps_parent, " + "ps_component, " + "ps_qty_per, " + "ps_effectivity, " + "ps_add_delete, " + "ps_ref " + "FROM ProdStrt " + "WHERE ps_parent = @ps_parent ORDER BY ps_component " ; SqlConnection mcs3Connection = MCS3_DB.GetConnection(); SqlCommand selectCmd = new SqlCommand(selectStmt, mcs3Connection); selectCmd.Parameters.Add("@ps_parent", parentPart); SqlDataAdapter psDataAdapter = new SqlDataAdapter(selectCmd); DataSet psDataSet = new DataSet(); psDataAdapter.Fill(psDataSet, "PS"); return psDataSet.Tables["PS"]; }

解决方案

You have two choices. You can set the ColumName of the respective column to whatever you want and if you do it before you bind, then it will show up. Or you can create a datagridtablestyle, and a datagridcolumnstyle and set it there. www.knowdotnet/articles/cgrid.html -- W.G. Ryan MVP Windows - Embedded Have an opinion on the effectiveness of Microsoft Embedded newsgroups? Let Microsoft know! www.windowsembeddedeval/...ity/newsgroups "Robert Schuldenfrei" <bo*@s-i-inc> wrote in message news:vI_5d.167178

3l3.13131@attbi_s03...

Hi NG: I have the following code working. The GetProdStrtList() returns a DataTable called psTable. A Grid control is successfully bound to this table and displays correctly. I would like to change the column headings to something other than the name of SQL column. For example I would prefer "Component" to the default "ps_component". I would also like to set the column width property myself. As you can see, I can set the grid''s caption by assigning "Components" to the .CaptionText property. I have not been able to set a column heading property. Thanks in advance. Cheers, Bob -- Robert Schuldenfrei S. I. Inc. 32 Ridley Road Dedham, MA 02026 bo*@s-i-inc 781/329-4828 //get and display the components in a grid DataTable psTable = ProdStrtTbl.GetProdStrtList(txtPartNo.Text); grdComponents.DataSource = psTable; grdComponents.CaptionText = "Components"; public static DataTable GetProdStrtList(string parentPart) { string selectStmt = "SELECT ps_parent, " + "ps_component, " + "ps_qty_per, " + "ps_effectivity, " + "ps_add_delete, " + "ps_ref " + "FROM ProdStrt " + "WHERE ps_parent = @ps_parent ORDER BY ps_component " ; SqlConnection mcs3Connection = MCS3_DB.GetConnection(); SqlCommand selectCmd = new SqlCommand(selectStmt, mcs3Connection); selectCmd.Parameters.Add("@ps_parent", parentPart); SqlDataAdapter psDataAdapter = new SqlDataAdapter(selectCmd); DataSet psDataSet = new DataSet(); psDataAdapter.Fill(psDataSet, "PS"); return psDataSet.Tables["PS"]; }

Thanks for William''s quick response! Hi Bob, First of all, I would like to confirm my understanding of your issue. From your description, I understand that you need to change the column name displayed on the datagrid table, when binding a datagrid to a DataTable. If there is any misunderstanding, please feel free to let me know. William has given us a good suggestion to use tablestyle. This simply changes the display name, but did not change the column name in the data source. Besides, we can also use ColumnMapping to achieve this. Before SqlDataAdapter.Fill, we just add a DataTableMapping and some DataColumnMapping objects to the TableMapping property of the SqlDataAdapter. This will change the name of DataColumn in the DataTable directly. When you bind to the DataGrid to the DataTable, the name is already changed. The following is an example, this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "Employees", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("EmployeeID", "IDofEmployee"), new System.Data.Common.DataColumnMapping("LastName", "Surname"), new System.Data.Common.DataColumnMapping("FirstName", "FirstName"), new System.Data.Common.DataColumnMapping("Title", "Position")})}); For more information, please check the following link: msdn.microsoft/library/de...us/cpref/html/ frlrfsystemdatacommondataadapterclasstablemappings topic.asp HTH. Kevin Yu ======= "This posting is provided "AS IS" with no warranties, and confers no rights."

更多推荐

DataGrid列标题

本文发布于:2023-11-05 18:10:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561508.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标题   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!