目录1、winform中datagridview增加行号2、WPF中datagrid增加行号3、WPF dev控件GridControl增加行号1、WinFORM中datagridv
在界面上拖一个控件dataGridView1
,在datagridview
添加行事件中添加如下代码:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
try
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
this.dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
}
catch
{
MessageBox.Show("处理异常:表格行标题添加异常");
}
}
这样表格中每次有新行增添就会被自动打标行号.
WPF
类似WinForm
中datagridview
的表格控件是datagrid
,我们可以将行标题添加代码写在LoadingRow
事件中:
①附件事件:
一般是在xmal窗体的cs初始化类中:
DG.LoadingRow += new EventHandler<DataGridRowEventArgs>(DG_LoadingRow);
CM框架mvvm模式下:
[Event LoadingRow]=[DG_LoadingRow($source,$eventArgs)]"
DG_LoadingRow事件如下:
private void DG_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}
dev
控件GridControl
没有行增添增添事件,我们可以用下面的方法去做:
增加控件引用空间:
xmlns:dxg="Http://schemas.devexpress.com/winfx/2008/xaml/grid"
<dxg:GridControl Name="grid" AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView RowIndicatorContentTemplate="{StaticResource rowIndicatorContentTemplate}"/>
</dxg:GridControl.View>
</dxg:GridControl
定义模板资源:
<UserControl.Resources>
<DataTemplate x:Key="rowIndicatorContentTemplate">
<StackPanel VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=RowHandle.Value}"
TextAlignment="Center"
Foreground="Gray"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
----------------------------------------------------
到此这篇关于C# datagridview
、datagrid
、GridControl
增加行号代码解析的文章就介绍到这了,更多相关C# datagridview
、datagrid
、GridControl
增加行号内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: C# datagridview、datagrid、GridControl增加行号代码解析
本文链接: https://www.lsjlt.com/news/154655.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0