C# 在设计时设置的Listview列宽在运行时未使用
我在设计时通过在listview控件中拖动列边框手动定义了列宽,但在运行时,所有列都以相同的宽度显示 InitialiseComponent中的代码为C# 在设计时设置的Listview列宽在运行时未使用,c#,listview,C#,Listview,我在设计时通过在listview控件中拖动列边框手动定义了列宽,但在运行时,所有列都以相同的宽度显示 InitialiseComponent中的代码为 // // lstLicenses // this.lstLicenses.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.Company, this.ExpiryDate,
//
// lstLicenses
//
this.lstLicenses.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.Company,
this.ExpiryDate,
this.MaxUsers,
this.Key});
this.lstLicenses.Location = new System.Drawing.Point(465, 46);
this.lstLicenses.MultiSelect = false;
this.lstLicenses.Name = "lstLicenses";
this.lstLicenses.Size = new System.Drawing.Size(565, 228);
this.lstLicenses.TabIndex = 18;
this.lstLicenses.UseCompatibleStateImageBehavior = false;
this.lstLicenses.View = System.Windows.Forms.View.Details;`
`
似乎没有存储设计时列宽信息。解决方法是在加载后手动定义列宽,如下所示,但这不是必需的
string queryString = "SELECT * FROM dbo.Licenses";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, sEnv);
DataSet Licenses = new DataSet();
adapter.Fill(Licenses, "Licenses");
int iLicensesRows = Licenses.Tables[0].Rows.Count;
foreach (DataRow row in Licenses.Tables[0].Rows)
{
ListViewItem LVI = lstLicenses.Items.Add(row["Company"].ToString());
LVI.SubItems.Add(row["ExpiryDate"].ToString());
LVI.SubItems.Add(row["MaxUsers"].ToString());
LVI.SubItems.Add(row["Key"].ToString());
}
lstLicenses.Columns[0].Width = 100;
lstLicenses.Columns[1].Width = 130;
lstLicenses.Columns[2].Width = 85;
lstLicenses.Columns[3].Width = 225;
有人能告诉我如何在运行时使用设计时列宽吗?问题:您正在通过以下语句手动设置
列表视图列的宽度
lstLicenses.Columns[0].Width = 100;
lstLicenses.Columns[1].Width = 130;
lstLicenses.Columns[2].Width = 85;
lstLicenses.Columns[3].Width = 225;
上面的语句覆盖了designtime的宽度
。因此,最后将根据上面的语句设置列表视图
的列宽
解决方案您应该注释/删除上述语句,以获得在设计时间设置的宽度
完整解决方案:
string queryString = "SELECT * FROM dbo.Licenses";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, sEnv);
DataSet Licenses = new DataSet();
adapter.Fill(Licenses, "Licenses");
int iLicensesRows = Licenses.Tables[0].Rows.Count;
foreach (DataRow row in Licenses.Tables[0].Rows)
{
ListViewItem LVI = lstLicenses.Items.Add(row["Company"].ToString());
LVI.SubItems.Add(row["ExpiryDate"].ToString());
LVI.SubItems.Add(row["MaxUsers"].ToString());
LVI.SubItems.Add(row["Key"].ToString());
}
//Comment or remove the below statements to get design time widths.
/*lstLicenses.Columns[0].Width = 100;
lstLicenses.Columns[1].Width = 130;
lstLicenses.Columns[2].Width = 85;
lstLicenses.Columns[3].Width = 225; */
看起来你甚至没有试过,我试过了,OP的问题显然存在。只有第一列保留设计时宽度,其他所有列不保留。顺便说一句,他发布的代码(你说它覆盖了设计宽度)是他试图保持宽度为他想要的,但当然不是在设计时。