DataGrid中的ItemDataBound [英] ItemDataBound in Datagrid

查看:122
本文介绍了DataGrid中的ItemDataBound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到数据集的数据网格,该数据集本身通过xml填充.

现在,由于通过不包含任何数据/空值的Xml节点填充了数据集,因此将其完全排除(如果我错了,请纠正我).但是,我需要知道数据是否为空,以便我可以在前端(网格)上以特定方式做出反应.

因此,我正在尝试的解决方案是使用Datagrid ItemDataBound事件.我试图确定该列是否存在,然后通过添加具有所需数据的列来做出相应的反应.

I have a datagrid which is bound to a dataset which itself gets filled via xml.

Now because the Dataset get filled via Xml nodes which contain no data/null are excluded completely(correct me if i''m wrong). I am however needing to know whether the data is null so that i may react in a particular way on the front end(grid).

So the solution i am attempting is by using the Datagrid ItemDataBound event. Wherein i am trying to determine if the column exists and then if not react accordingly by adding a column with the data i desire.

<pre lang="xml">Protected Sub grdDirectivesCancelling_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdDirectivesCancelling.ItemDataBound


          Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)

        Try
            If DRV.Row.Table.Columns.Contains("MyCustomColumn") Then
                Response.Write("<br>data in column value")
            Else
                ''add data column
                DRV.Row.Table.Columns.Add("MyCustomColumn")
                ''Response.Write("<br>no column value for MyCustomColumn")
            End If
        Catch ex As Exception
            Response.Write(ex)
        End Try



    End Sub





但是,我收到以下错误:

System.NullReferenceException:对象引用未设置为对象的实例.被





I however receive the following error:

System.NullReferenceException: Object reference not set to an instance of an object. being thrown by

If DRV.Row.Table.Columns.Contains(&quot;MyCustomColumn&quot;) Then


:-(

谢谢.


:-(

Thanks.

推荐答案

在条件中输入您的代码

如果是e.Item.DataItem,则

put ur code inside if condition

If e.Item.DataItem Then

Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)

        Try
            If DRV.Row.Table.Columns.Contains("MyCustomColumn") Then
                Response.Write("<br>data in column value")
            Else
                'add data column
                DRV.Row.Table.Columns.Add("MyCustomColumn")
                'Response.Write("<br>no column value for MyCustomColumn")
            End If
        Catch ex As Exception
            Response.Write(ex)
        End Try



如果结束

现在你尝试



End If

now u try




谢谢您的协助,

我已经按照您的建议添加了,尽管现在我又收到了另一个例外.

System.InvalidCastException: Conversion from type ''DataRowView'' to type ''Boolean'' is not valid

任何想法将不胜感激.

谢谢.
Hi,

Thank you for your assistance,

I have added as you suggested although i now receive another exception.

System.InvalidCastException: Conversion from type ''DataRowView'' to type ''Boolean'' is not valid

Any ideas would be appreciated.

Thank you.




您可以使用

IsDBNull
Hi,

you can use

IsDBNull


这篇关于DataGrid中的ItemDataBound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆