winform DataGrid 列宽度 如何设置?
程序如下,如何设置DataGrid每列的宽度?private void button1_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.Filter="Access files(*.mdb)|*.mdb";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
string str=this.openFileDialog1.FileName.ToString();
string strCon = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+str ;
OleDbConnection dbconn=new OleDbConnection(strCon);
dbconn.Open();
System.Data.OleDb.OleDbDataAdapter da=new OleDbDataAdapter("select * from countryA",strCon);
DataSet ds=new DataSet ();
da.Fill (ds,"table");
//读取
DataTable dt=ds.Tables["table"];
this.dataGrid1.DataSource=dt;
}
}
--------------------编程问答-------------------- 我也有这问题,帮顶 up --------------------编程问答-------------------- 这个要通过DataGridColumnStyle来设置指定列的宽度
DataGridTableStyle tb =new DataGridTableStyle();
DataGridTextBoxColumn col = new DataGridTextBoxColumn();
this.DataGrid1.DataSource = MyDataSet.Tables[0];
col.MappingName = "Filed1";
col.HeaderText = "列1";
col.Width = 100;
tb.GridColumnStyles.Add(col);
tb.MappingName = ds.Tables[0].TableName;
this.DataGrid1.TableStyles.Add(tb);
--------------------编程问答-------------------- //初始化DataGrid
private void InitDataGridColumnHeader(DataTable tb)
{
this.lstSaveForm.DataSource=tb;
DataGridTableStyle dts=new DataGridTableStyle();
dts.HeaderForeColor=System.Drawing.Color.FromArgb(0,0,0);
dts.HeaderBackColor=System.Drawing.Color.FromArgb(255,255,255);
dts.RowHeaderWidth=0;
dts.BackColor=Color.FromArgb(241,241,241);
dts.AlternatingBackColor=Color.White;
//注意:必须加上这一句,否则自定义列格式无法使用
dts.MappingName=tb.TableName;
DataGridTextBoxColumn colBatch = new DataGridTextBoxColumn();
colBatch.Width=40;
colBatch.HeaderText = "批次";
colBatch.MappingName = "sale_Draw_Batch";
colBatch.NullText="";
colBatch.TextBox.BackColor=Color.White;
colBatch.Alignment=HorizontalAlignment.Center;
dts.GridColumnStyles.Add(colBatch);
DataGridTextBoxColumn colTicketName = new DataGridTextBoxColumn();
colTicketName.Width=135;
colTicketName.NullText="";
colTicketName.Alignment=HorizontalAlignment.Left;
colTicketName.HeaderText = "门票名称";
colTicketName.TextBox.BackColor=Color.White;
colTicketName.MappingName = "ticket_Name";
dts.GridColumnStyles.Add(colTicketName);
DataGridTextBoxColumn colUser = new DataGridTextBoxColumn();
colUser.Width=70;
colUser.HeaderText = "领取人员";
colUser.NullText="";
colUser.MappingName = "sale_Draw_User";
colUser.Alignment=HorizontalAlignment.Center;
colUser.TextBox.BackColor=Color.White;
dts.GridColumnStyles.Add(colUser);
DataGridTextBoxColumn colDate = new DataGridTextBoxColumn();
colDate.Width=75;
colDate.NullText="";
colDate.HeaderText = "领取日期";
colDate.MappingName = "sale_Draw_Date";
colDate.Alignment=HorizontalAlignment.Center;
colDate.TextBox.BackColor=Color.White;
dts.GridColumnStyles.Add(colDate);
DataGridTextBoxColumn colNum = new DataGridTextBoxColumn();
colNum.Width=65;
colNum.NullText="";
colNum.HeaderText = "领取数量";
colNum.MappingName = "sale_Draw_Num";
colNum.TextBox.BackColor=Color.White;
colNum.Alignment=HorizontalAlignment.Center;
dts.GridColumnStyles.Add(colNum);
DataGridTextBoxColumn colStartCode= new DataGridTextBoxColumn();
colStartCode.Width=90;
colStartCode.NullText="";
colStartCode.HeaderText = "领取开始代码";
colStartCode.MappingName = "ticket_Start_Code";
colStartCode.TextBox.BackColor=Color.White;
colStartCode.Alignment=HorizontalAlignment.Center;
dts.GridColumnStyles.Add(colStartCode);
DataGridTextBoxColumn colEndCode = new DataGridTextBoxColumn();
colEndCode.Width=90;
colEndCode.NullText="";
colEndCode.ReadOnly=true;
colEndCode.HeaderText = "领取结束代码";
colEndCode.MappingName = "ticket_End_Code";
colEndCode.TextBox.BackColor=Color.White;
colEndCode .Alignment=HorizontalAlignment.Center;
dts.GridColumnStyles.Add(colEndCode);
this.lstSaveForm.TableStyles.Clear();
this.lstSaveForm.TableStyles.Add(dts);
} --------------------编程问答-------------------- DataGridTableStyle
用界面做也行 --------------------编程问答-------------------- 我也有这样的问题,好像楼的不好用。帮顶! --------------------编程问答--------------------
经过证明这个可以用。环境vs2003,winform。 --------------------编程问答--------------------
+1这个是可以的
补充:.NET技术 , C#