SilverLight实现自动行号

首先看效果



思路:

在每个datagrid的第一列加上一个模板列,里面只定义一个textblock

之后在datagrid的行加载事件中,给每个行的textblock加上一个编号。

xaml代码:

View Code
<data:DataGrid x:Name="gridList" Grid.Row="0" Margin="5,0,5,0" GridLinesVisibility="All"
AutoGenerateColumns
="False"
FrozenColumnCount
="1"
AreRowDetailsFrozen
="True"
HorizontalAlignment
="Stretch" CanUserSortColumns="False"
ItemsSource
="{Binding InvStockListModelList,Mode=TwoWay}"
IsReadOnly
="True" SelectedItem="{Binding InvStockListModel,Mode=TwoWay}">

<data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="编号">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTextColumn Header="物料名称" Binding="{Binding MaterialModel.Mat_Name,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="仓库名称" Binding="{Binding StorgeModel.Storge_Name,Mode=TwoWay}"/>

<data:DataGridTextColumn Header="库位名称" Binding="{Binding InvLocationModel.Location_Name,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="批号" Binding="{Binding StockList_Batch,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="年度" Binding="{Binding StockList_Year,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="会计期间" Binding="{Binding StockList_Period,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="收入数量" Binding="{Binding StockList_IAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="收入单价" Binding="{Binding StockList_IPrice,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="收入金额" Binding="{Binding StockList_IMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="发出数量" Binding="{Binding StockList_OAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="发出单价" Binding="{Binding StockList_OPrice,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="发出金额" Binding="{Binding StockList_OMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="期末数量" Binding="{Binding StockList_EndAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="期末金额" Binding="{Binding StockList_EndMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="供应商批号" Binding="{Binding StockList_SuppBatch,Mode=TwoWay}"/>
</data:DataGrid.Columns>
</data:DataGrid>

cs代码:

View Code
public InvStockListView()
{
InitializeComponent();
gridList.LoadingRow
+= new EventHandler<DataGridRowEventArgs>(gridList_LoadingRow);
}
void gridList_LoadingRow(object sender, DataGridRowEventArgs e)
{
var index
= e.Row.GetIndex();
var cell
= gridList.Columns[0].GetCellContent(e.Row) as TextBlock;
if (cell != null) cell.Text = (index + 1).ToString();
}

大功告成!!!


作者:记忆逝去的青春
出处:http://www.cnblogs.com/lukun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过http://www.cnblogs.com/lukun/  联系我,非常感谢。

posted on 2011-04-02 11:30  记忆逝去的青春  阅读(959)  评论(2编辑  收藏  举报