【问题标题】:Listview column widths set at design time not used at run time在设计时设置的 Listview 列宽在运行时未使用
【发布时间】:2013-11-29 03:30:25
【问题描述】:

我在设计时通过在列表视图控件中拖动列边框手动定义了列宽,但在运行时所有列都以相同的宽度显示。

InitialiseComponent 中的代码是

// 
        // 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;

谁能告诉我如何获取设计时列宽以在运行时使用?

【问题讨论】:

    标签: c# listview


    【解决方案1】:

    问题:您正在通过以下语句再次手动设置您的ListView columnswidths

                    lstLicenses.Columns[0].Width = 100;
                    lstLicenses.Columns[1].Width = 130;
                    lstLicenses.Columns[2].Width = 85;
                    lstLicenses.Columns[3].Width = 225;
    

    上面的语句会覆盖你的设计时间Widths.so 最后你的ListView 列宽是根据上面的语句设置的。

    解决方案您应该注释/删除上述语句以获得设置为designtimeWidths

    完整的解决方案:

                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 的问题。只有第一列保留设计时宽度,所有其他列不保留。顺便说一句,他发布的代码(你说它覆盖了设计宽度)是他试图保持宽度为他想要的,但当然这不是在设计时。
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 2012-07-13
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多