datagridview中只要第3行到第8行的列显示图片。怎么做?我用DataGridViewImageColumn这个,会使得一整列都显示图片。

解决方案 »

  1.   

    在行绑定事件里控制,或者嵌套img标签!
      

  2.   


    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if ((e.RowIndex < 2 || e.RowIndex > 7) && 
                    e.ColumnIndex == 显示图片的列索引)
                {
                    e.Value = null;
                }
            }
      

  3.   

    或者这样,你显示图片的列可以以路径的形式呈现
     private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.RowIndex > 2 && e.RowIndex < 7 && 
                    e.ColumnIndex == 显示图片的列索引)
                {
                    string path = e.Value.ToString();
                    e.Value = GetImage(path);
                }
            }      
            public System.Drawing.Image GetImage(string path)
            {
                System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
                System.Drawing.Image result = System.Drawing.Image.FromStream(fs);            fs.Close();            return result;        }
      

  4.   

    我是这样做的: 
    DataTable dt = new DataTable();
                dt.Columns.Add("1");
                dt.Columns.Add("2");            DataRow dr=dt.NewRow();
                dr["1"] = "11";
                dr["2"] = @"C:\Documents and Settings\10528\桌面\12\DataGridDemo2\Resources\Image2.bmp";
     dt.Rows.Add(dr); dataGridView2.Rows.Add(10);DataGridViewImageColumn imagecolum = new DataGridViewImageColumn();
                imagecolum.HeaderText = "图片列";            DataGridViewImageCell imagecell = new DataGridViewImageCell();
                
                dataGridView2.Columns.Insert(1, imagecolum);
      for (int i = 0;i<dt.Rows.Count ;i++ )
                {
                    dataGridView2.Rows[i+1].Cells[0].Value = dt.Rows[i]["1"].ToString();
                    dataGridView2.Rows[i+1].Cells[1].Value =Image.FromFile(dt.Rows[i]["2"].ToString());               
                }//加上tanghuawei说的方法
     private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
               if ((e.RowIndex < 2 || e.RowIndex > 7) && 
                    e.ColumnIndex == 1)
                {
                    e.Value = null;
                }
            }
     
    还是有,只不过是没有图片预览的图片。