如何在 VB.net 中向 DataGridView 添加 UserControl,并始终显示该控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32280673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a UserControl to a DataGridView in VB.net, and have the control always showing?
提问by JPeroutek
I made a usercontrol that is basically a panel with a bunch of labels, buttons, and Event Handlers on it. I want to be able to populate a DataGridView with these controls.
我制作了一个用户控件,它基本上是一个面板,上面有一堆标签、按钮和事件处理程序。我希望能够使用这些控件填充 DataGridView。
I have followed the instructions in this MSDN article, and have created a DataGridViewColumn and a DataGridViewCell class to associate with my Control.
我已经按照这篇 MSDN 文章中的说明进行操作,并创建了一个 DataGridViewColumn 和一个 DataGridViewCell 类来与我的控件关联。
The issue I am having is that the control is not rendering. When I run the code at the link above, It shows the date in the cell, and the user control only shows when the cell is being edited. What I'd like to have happen is that the control is always visible in the cell, similar to the behavior in a DataGridViewImageColumn.
我遇到的问题是控件未呈现。当我在上面的链接上运行代码时,它会在单元格中显示日期,而用户控件仅在单元格被编辑时显示。我希望发生的是控件在单元格中始终可见,类似于 DataGridViewImageColumn 中的行为。
How can I force the DataGridView to always show the control?
如何强制 DataGridView 始终显示控件?
回答by Reza Aghaei
The main idea:
主要思想:
- Create a user control
- In form load event, for each record, create an instance of user control, set values to its properties, add event handlers, set its visiblity to false and then add it to your grid Controls collection, and set the tag of your wanted cell or that row to this control.
- Handle CellPaintingevent of your grid and for each row of grid, retrieve tag of your wanted cell and convert it to your control and position it in cell bounds and make it visible. To get the position that control should be shown use GetCellDisplayRectanglemethod of DataGridView
- 创建用户控件
- 在表单加载事件中,为每条记录创建一个用户控件实例,为其属性设置值,添加事件处理程序,将其可见性设置为 false,然后将其添加到您的网格控件集合中,并设置您想要的单元格的标签或到这个控件的那一行。
- 处理网格的CellPainting事件和网格的每一行,检索所需单元格的标签并将其转换为您的控件并将其定位在单元格边界中并使其可见。要获取控件应显示的位置,请使用DataGridView 的GetCellDisplayRectangle方法
Screenshot:
截屏:
My sample control with name of SomeControl, contains a label and a text box and a property SomeProperty and an event SomePropertyChanges. I show cell value in TextBox of my custom control.
我的示例控件名为 SomeControl,包含一个标签和一个文本框以及一个属性 SomeProperty 和一个事件 SomePropertyChanges。我在自定义控件的 TextBox 中显示单元格值。
You can do anything with your custom control containing any control and any type of properties and events.
您可以对包含任何控件和任何类型的属性和事件的自定义控件执行任何操作。
Code:
代码:
Here is vb-like pseudo code.
这是类似 vb 的伪代码。
'This is not VB, this is vb-like pseudo code
Private Sub Form_Load(sender as object , e as EventArgs)
'Get data
'Show data in grid
'For each row
For Each row as DataGridViewRow in this.categoryDataGridView.Rows
'Create an instance of control
Dim myControl as New SomeControl()
'Set properties and register event handlers
myControl.SomeProperty = row.Cells[2].Value
myControl.SomePropertyChanged += myControl_SomePropertyChanged
'Make it invisible
myControl.Visible = False
'Set tag of your wanted cell to control
row.Cells[2].Tag = myControl
'Add control to Controls collection of grid
this.categoryDataGridView.Controls.Add(myControl)
Next
End Sub
Private Sub myControl_SomePropertyChanged(sender as object, e as EventArgs)
'event handler of SomePropertyChanged for custom control
'do stuff here
End Sub
Private Sub dataGridView_CellPainting(sender as object, e as DataGridViewCellPaintingEventArgs )
'for each row
For i as Integer=0 To dataGridView.RowCount- 1
'Extract control from tag of your wanted cell
var myControl= this.dataGridView.Rows[i].Cells[2].Tag as SomeControl
'Get cell rectangle
Dim cellRectangle As Rectangle= dataGridView.GetCellDisplayRectangle(2, i, True)
'Set location
myControl.Location = New Point(cellRectangle.X, cellRectangle.Y )
'Set size
myControl.Size = New Size(cellRectangle.Width - 1, cellRectangle.Height - 1)
'Make visible
myControl.Visible = True
Next
}
回答by mohamed Tayel
read this article How can you add a custom user control to a datagridview? http://bytes.com/topic/visual-basic-net/answers/444528-how-can-you-add-custom-user-control-datagridview
阅读本文如何将自定义用户控件添加到 datagridview? http://bytes.com/topic/visual-basic-net/answers/444528-how-can-you-add-custom-user-control-datagridview
回答by Ravindra Maurya
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Email Id");
for (int j = 0; j < 10; j++)
{
dt.Rows.Add("", "[email protected]");
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns[0].Width = 200;
/*
* First method : Convert to an existed cell type such ComboBox cell,etc
*/
DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
ComboBoxCell.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });
this.dataGridView1[0, 0] = ComboBoxCell;
this.dataGridView1[0, 0].Value = "bbb";
DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
this.dataGridView1[0, 1] = TextBoxCell;
this.dataGridView1[0, 1].Value = "some text";
DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();
CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dataGridView1[0, 2] = CheckBoxCell;
this.dataGridView1[0, 2].Value = true;
DataGridViewTextBoxCell txt = new DataGridViewTextBoxCell();
txt.ToolTipText = "Enter Ur Name";
this.dataGridView1[0, 3] = txt;
this.dataGridView1[0, 3].Value = "lakshman";
/*
* Second method : Add control to the host in the cell
*/
DateTimePicker dtp = new DateTimePicker();
dtp.Value = DateTime.Now.AddDays(-10);
//add DateTimePicker into the control collection of the DataGridView
this.dataGridView1.Controls.Add(dtp);
//set its location and size to fit the cell
dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3, true).Location;
dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3, true).Size;
TextBox Text = new TextBox();
Text.Text = "This my Name";
Text.BackColor = Color.RosyBrown;
this.dataGridView1.Controls.Add(Text);
Text.Location = this.dataGridView1.GetCellDisplayRectangle(0, 4, true).Location;
Text.Size = this.dataGridView1.GetCellDisplayRectangle(0, 4, true).Size;
}


