は、私は自分のミス発見してきた質問の編集ではなぜAsp.Net GridViewのためのRowDataBoundコールバック内のDataRow [ "columnValue"]ヌル
更新です(string
に不正なキャストを。)
多くすべてのおかげで、すべての提案が最も役立ちました。
元の質問
私はDataTable
を含むDataSet
で満たされているObjectDataSource
から.aspx
GridView
のページ、データバインドされています。
GridView
のRowDataBound
コールバックを処理する場合、基になるデータセットの列を列名で参照することはできませんが、タイプがDBNull
のオブジェクトが返されます。だから、:
protected void accountsGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// One way:
// This works, but uses a hard coded column index which
// I want to replace.
if (e.Row.Cells[11].Text != " ")
{
// Apply certain styles to the row depending on
// the contents of the cell.
}
// Another way. This doesn't work - "date" is always DBNull.
DataRow row = (e.Row.DataItem as DataRowView).Row;
DateTime date;
if(!Convert.IsDBNull(row["date"]))
{
date = (DateTime)row["date"]);
if (date < SomeConstantDate)
{
// Apply certain styles to the row depending on
// the contents of the cell.
}
}
}
}
だから、特に:
row["date"]
はタイプDBNull
のオブジェクトを返します。row[11]
は、DateTime
を返します。row.Table.Columns["date"]
が有効DataColumn
row[row.Table.Columns["date"]]
タイプDBNull
のオブジェクトを返し。
これはどうして起こっているのかもしれないのでしょうか?
セットアップ
<asp:GridView ID="GV" runat="server" DataSourceID="ODS"
AutoGenerateColumns="false"
... snip ...
onrowdatabound="GV_RowDataBound" >
<Columns> ... </Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ODS" runat="server"
SelectMethod="getData"
... snip ...
</asp:ObjectDataSource>
あなたは、列名は日付だけでなく、フィールド名であることを確認しましたか? –