C# datagridview、datagrid、GridControl增加行號(hào)代碼解析
1、WinForm中datagridview增加行號(hào)
在界面上拖一個(gè)控件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("處理異常:表格行標(biāo)題添加異常");
}
}
這樣表格中每次有新行增添就會(huì)被自動(dòng)打標(biāo)行號(hào).
2、WPF中datagrid增加行號(hào)
WPF類似WinForm中datagridview的表格控件是datagrid,我們可以將行標(biāo)題添加代碼寫在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;
}
3、WPF dev控件GridControl增加行號(hào)
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>
----------------------------------------------------
到此這篇關(guān)于C# datagridview、datagrid、GridControl增加行號(hào)代碼解析的文章就介紹到這了,更多相關(guān)C# datagridview、datagrid、GridControl增加行號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
C#如何在窗體程序中操作數(shù)據(jù)庫數(shù)據(jù)
這篇文章主要介紹了C#如何在窗體程序中操作數(shù)據(jù)庫數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
C#實(shí)現(xiàn)窗體抖動(dòng)的兩種方法
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)窗體抖動(dòng)的兩種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
C#實(shí)現(xiàn)實(shí)體類和XML相互轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)實(shí)體類和XML相互轉(zhuǎn)換的資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
C#使用DeepSeek?API實(shí)現(xiàn)自然語言處理,文本分類和情感分析
在C#中使用DeepSeek?API可以實(shí)現(xiàn)多種功能,例如自然語言處理、文本分類、情感分析等,本文主要為大家介紹了具體實(shí)現(xiàn)步驟,需要的可以了解下2025-02-02

