DataGridViewImageColumn图像布局不起作用

时间:2015-04-30 14:07:57

标签: c# datagridview imagelist datagridviewimagecolumn

我正在尝试使用image列在datagridview上显示图像;然而, 它看起来垂直拉伸并水平压扁。我不确定是什么限制它以这种方式显示。

网格生成

            gvPackage.Columns.Clear();

            gvPackage.AutoGenerateColumns = false;
            DataGridViewColumn[] cols = new DataGridViewColumn[5];
            GridColumn gc = null;

            cols[0] = new DataGridViewTextBoxColumn();
            cols[0].DataPropertyName = "ID";
            cols[0].Name = "ID";
            cols[0].Visible = false;

            cols[1] = new DataGridViewTextBoxColumn();
            cols[1].DataPropertyName = "iLive";
            cols[1].Name = "iLive";
            cols[1].Visible = false;

            cols[2] = new DataGridViewTextBoxColumn();
            cols[2].DataPropertyName = "iExplicit";
            cols[2].Name = "iExplicit";
            cols[2].Visible = false;

            cols[3] = new DataGridViewImageColumn();
            cols[3].Name = "Icon";
            cols[3].Width = 70;
            cols[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            cols[3].FillWeight = 1F;

            cols[4] = new DataGridViewTextBoxColumn();
            cols[4].DataPropertyName = "sName";
            cols[4].Name = "sName";
            cols[4].Width = 216;
            cols[4].FillWeight = 50.5166F;

            gvPackage.Columns.AddRange(cols);

            List<Package> _pl = _______Manager.SHARED_RES;

            foreach (Package p in _pl)
            {
                gvPackage.Rows.Add(
                    p.ID,
                    p.iLive,
                    p.iExplicit,
                    gImageList.Images["imgNoExplicitNoLive"],
                    p.sName
                    );
            }

单元格格式:

 private void gvPackage_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gvPackage.Columns[e.ColumnIndex].Name == "Icon")
            {
                bool isExp = this.gvPackage.Rows[e.RowIndex].Cells[2].Value.ToString() == "1";
                bool isLive = this.gvPackage.Rows[e.RowIndex].Cells[1].Value.ToString() == "1";
                if (isExp && isLive)
                {
                    e.Value = gImageList.Images["imgExplicitLive"];

                }
                else
                    if (!isExp && isLive)
                    {
                        e.Value = gImageList.Images["imgNoExplicitLive"];
                    }
                    else
                        if (isExp && !isLive)
                        {
                            e.Value = gImageList.Images["imgExplicitNoLive"];
                        }
                        else
                            if (!isExp && !isLive)
                            {
                                e.Value = gImageList.Images["imgNoExplicitNoLive"];
                            }
            }
        }

图片不好:

Incorrect image

源图片: Source image

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

首先查看gImageList.Imagesize

默认情况下,它将使用16x16的方形图像!

在加载之前将其更改为Images 的大小

gImageList.ImageSize = new Size(61,12);

请注意所有 Images需要相同大小。如果没有,你应该改变它们;比ImageList适用的拉伸要好得多!

另请注意,Images中的ImageList 限制256x256像素。

您可能还想检查一下您对ColorDepth = Depth8Bit

是否满意

显然,您的Column/Cell也需要提供足够的空间!