[英]Datagridview column in C#?
how can i get a column to be able to contain a textbox or an image? 如何让列能够包含文本框或图像? I am trying to add any type of data to that one column, wether it be text or an image. 我试图将任何类型的数据添加到该列,无论是文本还是图像。 How would i do this in C#? 我如何在C#中做到这一点?
ok, programmatically, i am going to have both textboxes and images in the same column...how do i do it? 好的,以编程方式,我将在同一列中同时拥有文本框和图像...我该怎么做?
Here is an example. 这是一个例子。 However you will need to handle the default image (in absence it is going to have a cross marked image). 但是,您将需要处理默认图像(如果没有它将有一个交叉标记的图像)。 Refer to this MSDN link 请参阅此MSDN链接
private void Form1_Load(object sender, EventArgs e)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Image");
dataTable.Rows.Add("Desert", @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
dataTable.Rows.Add("Tulips", @"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg");
dataTable.AcceptChanges();
DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn();
textColumn.DataPropertyName = "Name";
textColumn.HeaderText = "Name";
textColumn.Width = 100;
dataGridView1.Columns.Add(textColumn);
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn.DataPropertyName = "Image";
imageColumn.HeaderText = "Image";
imageColumn.Width = 100;
dataGridView1.Columns.Add(imageColumn);
dataGridView1.DataSource = dataTable;
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewImageColumn)
{
string imagePath = (e.Value ?? "").ToString().Trim();
if(System.IO.File.Exists(imagePath))
e.Value = Image.FromFile(imagePath);
}
}
To add a TextBox
control inside the DataGridView
, click the smart tag and select Add Column. 要在DataGridView
添加TextBox
控件,请单击智能标记并选择“添加列”。 Select DataGridViewTextColumn
and click Add. 选择DataGridViewTextColumn
,然后单击“添加”。 Select DataGridViewImageColumn
if you choose image. 如果选择图像,请选择DataGridViewImageColumn
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.