C# 将新列中的按钮添加到DataGrid中的所有行
我有一个C# 将新列中的按钮添加到DataGrid中的所有行,c#,button,datagridview,row,C#,Button,Datagridview,Row,我有一个DataGridView对象: dataGridView1.DataSource = an.peaks; (a.peaks是一个列表对象。点类型有3个属性:x、y、z) witch在运行时生成下一个表:(显然我还不能上传图像,因为我是新用户,所以我将尝试绘制它:) _124;124; 124; 124; 124;_124;_124;_124;_124;_124;_124;_124;_124;[新专栏] ____|_11_124;u12_124;u13_124;[text/button]
DataGridView
对象:
dataGridView1.DataSource = an.peaks;
(a.peaks是一个列表
对象。点
类型有3个属性:x、y、z)
witch在运行时生成下一个表:(显然我还不能上传图像,因为我是新用户,所以我将尝试绘制它:)
_124;124; 124; 124; 124;_124;_124;_124;_124;_124;_124;_124;_124;[新专栏]
____|_11_124;u12_124;u13_124;[text/button]您可以添加到WinForms项目并使用DataGrid。例如:
<DataGrid x:Name="grid" AutoGenerateColumns="False" x:FieldModifier="private">
<DataGrid.Resources>
<local:PointDataTemplateSelector x:Key="pointDataTemplateSelector" />
<DataTemplate x:Key="buttonTemplate">
<Button Click="OnAddButtonClick" Tag="{Binding Mode=OneWay}">Add</Button>
</DataTemplate>
<DataTemplate x:Key="textTemplate">
<TextBlock>Exists</TextBlock>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="X" Binding="{Binding X}" />
<DataGridTextColumn Header="Y" Binding="{Binding Y}" />
<DataGridTemplateColumn Header="Select" CellTemplateSelector="{StaticResource pointDataTemplateSelector}"></DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
这可能比WinForms更简单。使用DataGridView
实际上非常简单。您需要做的是:
添加DataGridViewButtonColumn类型的列
是标准的DataGridView
列类型。它可以通过设计器添加,但我通常更喜欢使用代码(通常以构造函数的形式)
设置UseColumnTextForButtonValue
true意味着Text
属性将应用于所有按钮,为它们提供“添加”按钮文本。您还可以使用DataPropertyName
指向网格数据源中的一列以提供按钮文本,甚至可以直接设置每个单元格的值
将按钮更改为文本
一旦你有了按钮栏,你就会想把特定的按钮变成文本。为此,可以将按钮类型的单元格替换为文本类型的单元格。您可以在许多地方执行此操作,但最好的方法之一是在DataBindingComplete
事件处理程序中—此事件在网格绑定并准备显示后,但在绘制之前触发
下面我只抓取索引为1的行,但您也可以检查每行的Value
属性
void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.Rows[1].Cells["MyButton"] = new DataGridViewTextBoxCell();
}
响应按钮点击
问题的最后一部分是对按钮点击的响应。这有点笨拙-您需要对整个网格使用CellClick
事件或EditingControlShowing
事件
- 蜂鸣器
private void DataGridView1_CellClick(object sender,
System.Windows.FormsDataGridViewCellEventArgs e)
{
if (DataGridView1.Columns[e.ColumnIndex].Name == "MyButton")
{
// button clicked - do some logic
}
}
- 编辑控制显示
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is Button)
{
Button btn = e.Control as Button;
btn.Click -= new EventHandler(btn_Click);
btn.Click += new EventHandler(btn_Click);
}
}
void btn_Click(object sender, EventArgs e)
{
int col = this.dataGridView1.CurrentCell.ColumnIndex;
int row = this.dataGridView1.CurrentCell.RowIndex;
// Rest of the logic goes here!
}
在您的情况下,编辑控件方法可能是最好的,因为它不会对单击已被文本替换的按钮作出响应。这种方式更像是对表单上任何其他按钮的响应方式 看一看[如何:在Windows窗体DataGridView单元格中托管控件],我希望我之前读过(我没有收到回答问题的电子邮件-必须更改设置…)。无论如何,我使用了一种不同的方法,使用ListBox。虽然不太“专业”,但有点“审美”。如果我有时间,也许我会回去换衣服。谢谢。@Igor如果你觉得这个答案或Unril很有用,给他们一个投票。试过了……我不行!:)我只差1分就可以投赞成票了:D@Igor,哦,好吧:)主要是因为你发现了一些有用的东西而感到高兴。是你的头撞到墙上直到它断了(墙,是吗?!)还是你找到了一条旁路。我选择第二个是因为时间有限;)Windows窗体中的WPF主机控件是好模式吗?性能问题?
void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.Rows[1].Cells["MyButton"] = new DataGridViewTextBoxCell();
}
private void DataGridView1_CellClick(object sender,
System.Windows.FormsDataGridViewCellEventArgs e)
{
if (DataGridView1.Columns[e.ColumnIndex].Name == "MyButton")
{
// button clicked - do some logic
}
}
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is Button)
{
Button btn = e.Control as Button;
btn.Click -= new EventHandler(btn_Click);
btn.Click += new EventHandler(btn_Click);
}
}
void btn_Click(object sender, EventArgs e)
{
int col = this.dataGridView1.CurrentCell.ColumnIndex;
int row = this.dataGridView1.CurrentCell.RowIndex;
// Rest of the logic goes here!
}