简体   繁体   English

在设计时设置的Listview列宽在运行时不使用

[英]Listview column widths set at design time not used at run time

I've defined column widths manually by dragging column borders in a listview control at design time but at run time all the columns appear with the same width. 我已经在设计时通过拖动列表视图控件中的列边框来手动定义列宽,但是在运行时,所有列都以相同的宽度出现。

Code in InitialiseComponent is 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;`

` It seems as though the design time column width information isn't getting stored. `似乎没有存储设计时间列宽信息。 Workaround is to define column widths manually after load as shown below but this shouldn't be necessary. 解决方法是在加载后手动定义列宽,如下所示,但这不是必需的。

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;

Can anyone tell me how to get the design time column widths to be used at run time? 谁能告诉我如何在运行时获取设计时列的宽度?

Problem : you are setting the widths of your ListView columns onceagain manually by following Statements: 问题:您正在通过以下语句再次手动设置ListView columnswidths

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

the above statements override your designtime Widths .so finally your ListView Column widths are set based on above statements. 上面的语句覆盖了您的设计时Widths最终,您的ListView Column宽度是根据上面的语句设置的。

Solution you should comment/remove the above statements to get the Widths which are set at designtime . 解决方法你应该评论/删除上述语句,以获得Widths被设定在designtime

Comlete Solution: 完整解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM