要將Color屬性值替換爲另一個Color屬性值,可以使用rowdatabound來完成。
當您設置自動生成列=「假」
列:指數,顏色,位置和SrNo將在DataGrid中顯示
你說你有另一種顏色是誰的價值應更換color列在DataGrid 。如果我是正確的,你可以通過以下操作...
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding SourceCollection}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Index}"/>
<asp:TemplateField HeaderText="Colour" SortExpression="Colour">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
<ItemTemplate>
<asp:Label ID="lblColor" Text='<%# Bind("Colour") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<DataGridTextColumn Binding="{Binding Location}"/>
<DataGridTextColumn Binding="{Binding Srno}"/>
</DataGrid.Columns>
</DataGrid>
VB.net:
Protected Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound
If e.Row.RowType = DataControlRowType.Header Or e.Row.RowType = DataControlRowType.DataRow Then
End If
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblColor1 As Label
lblColor1 = TryCast(e.Row.FindControl("lblColor"), Label)
lblColor1.Text = dtData.Rows(e.row.rowindex).ItemArray(0).tostring() '
' ItemArray Defined the Column Position. here give your Another Colour Column Value
End If
End Sub
C#.NET:
protected void GridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header | e.Row.RowType == DataControlRowType.DataRow) {
}
if (e.Row.RowType == DataControlRowType.DataRow) {
Label lblColor1 = default(Label);
lblColor1 = e.Row.FindControl("lblColor") as Label;
lblColor1.Text = dtData.Rows(e.Row.RowIndex).ItemArray(0).tostring();
//
// ItemArray Defined the Column Position. here give your Another Colour Column Value
}
}
在網格中將AutoGenerateColumns設置爲false。 –