代码如下:
private void button1_Click(object sender, EventArgs e)
{
string strsql = "select * from VIP";
SqlConnection conn = new SqlConnection("server=(local);database=Logistics;uid=sa;password=;");
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}显出出来列名是表中的列名,我自定义一个名为ID的Column1,我想表中的username显示在Column1下面。麻烦给出代码并简单说明下。
private void button1_Click(object sender, EventArgs e)
{
string strsql = "select * from VIP";
SqlConnection conn = new SqlConnection("server=(local);database=Logistics;uid=sa;password=;");
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}显出出来列名是表中的列名,我自定义一个名为ID的Column1,我想表中的username显示在Column1下面。麻烦给出代码并简单说明下。
select * ,username as ID from VIP
//定制表样式
private void button1_Click(object sender, EventArgs e)
{
string strsql = "select * from VIP";
SqlConnection conn = new SqlConnection("server=(local);database=Logistics;uid=sa;password=;");
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"vip");
dataGridView1.DataSource = ds.Tables["vip"];
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.HeaderFont = new Font("宋体",9F);
tableStyle.MappingName = "vip"; //必须设置MappingName,mapping为数据源表的别名否则不能显示。
dgTest.TableStyles.Add(tableStyle); //将样式添加到DataGrid中去。 //定制列样式
DataGridColumnStyle colStyle = tableStyle.GridColumnStyles["username"]; //绑定的数据源表中的列名
colStyle.Alignment = HorizontalAlignment.Center;
colStyle.HeaderText = "ID"; //显示的表头文字
colStyle.ReadOnly = true;
colStyle.Width = dgTest.Width/4; // 列的宽度设置 }试试看上面的代码
也可以在属性窗口对datagrid 进行样式设置
看看这也行 http://www.guo86.cn/post/16.html