DataGrid 的列可以是动态的吗 ?

girlvia 2002-03-16 02:23:21

数据源的列是动态的,我的SELECT 语句是动态拼出来的 。

和 EXCEL 里的那种数据透视表一样(也叫交叉汇总表)。

如果可以 ,那这些动态列的列名 C# 将是什么 ?

...全文
29 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
acptvc 2002-04-01
  • 打赏
  • 举报
回复
Hi Girlvia,

感谢您使用微软产品!

在默认情况下,DataGrid显示的列名就是你SQL语句中所选择字段的字段名。不过,你也可以用在所选的字段后面加上"as" 关键字来指定一个新名字,比如:

Select nID as "Employee ID" from .....

这样显示的不再是"nID",而是"Employee ID"。

-微软全球技术中心 VC技术支持

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
springmin 2002-04-01
  • 打赏
  • 举报
回复
sql语句列名
hehongyu2000 2002-03-17
  • 打赏
  • 举报
回复
可以, 动态列的名字默认是field name.
MSDN中的sample:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

public class DataGridSample:Form{
DataSet ds;
DataGrid myGrid;

static void Main(){
Application.Run(new DataGridSample());
}

public DataGridSample(){
InitializeComponent();
}

void InitializeComponent(){
this.ClientSize = new System.Drawing.Size(550, 450);
myGrid = new DataGrid();
myGrid.Location = new Point (10,10);
myGrid.Size = new Size(500, 400);
myGrid.CaptionText = "Microsoft .NET DataGrid";
this.Text = "C# Grid Example";
this.Controls.Add(myGrid);
ConnectToData();
myGrid.SetDataBinding(ds, "Suppliers");
}
void ConnectToData(){
// Create the ConnectionString and crete a SqlConnection.
// Change the data source value to the name of your computer.

string cString = "user id=sa;" +
"password=;" +
"initial catalog=northwind;" +
"data source=MyComputerName\\NetSDK;" +
"Connect Timeout=5";
SqlConnection myConnection = new SqlConnection(cString);
// Create a SqlDataAdapter.
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.TableMappings.Add("Table", "Suppliers");
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers",
myConnection);
myCommand.CommandType = CommandType.Text;

myAdapter.SelectCommand = myCommand;
Console.WriteLine("The connection is open");
ds = new DataSet("Customers");
myAdapter.Fill(ds);
// Create a second Adapter and Command.
SqlDataAdapter adpProducts = new SqlDataAdapter();
adpProducts.TableMappings.Add("Table", "Products");
SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products",
myConnection);
adpProducts.SelectCommand = cmdProducts;
adpProducts.Fill(ds);
myConnection.Close();
Console.WriteLine("The connection is closed.");
System.Data.DataRelation dr;
System.Data.DataColumn dc1;
System.Data.DataColumn dc2;
// Get the parent and child columns of the two tables.
dc1 = ds.Tables["Suppliers"].Columns["SupplierID"];
dc2 = ds.Tables["Products"].Columns["SupplierID"];
dr = new System.Data.DataRelation("suppliers2products", dc1, dc2);
ds.Relations.Add(dr);
}
}

110,538

社区成员

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

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

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