DataGridItem.DataItem可用性? [英] DataGridItem.DataItem availability?

查看:59
本文介绍了DataGridItem.DataItem可用性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我被DataGridItem.DataItem属性纠缠在一起。我试图调用

该属性来获取并显示绑定到当前所选行的

的内容。但是,有一个例外让我感到惊讶,它说

对象引用未设置为对象的实例。怎么可能发生?

我已经设置了数据源,并调用了DataBind()。

我查找了MSDN文档,并找到了一些不必要的

描述和代码示例。我自己并没有尝试这个例子,但是我注意到了一个不同之处,即示例使用单文件代码(即代码

写在。 aspx文件)我在代码隐藏

模式下编写代码。所以我想知道这是不是这个原因?但是我担心我会感到困惑

如果MS说DataItem属性不能通过代码隐藏获得:)

请帮忙吗?

解决方案

代码的位置没有区别。


从代码中发布相关片段和.aspx文件,可能那里有

a问题。


Eliyahu


" Laser Lu" < LA ****** @ hotmail.com>在消息中写道

news:u2 ************* @ TK2MSFTNGP15.phx.gbl ...

全部,该属性来获取并显示绑定到当前所选行的内容。但是,一个例外让我感到惊讶,它说对象引用未设置为对象的实例。它怎么会发生?我已经设置了数据源,并调用了DataBind()。
我查找了MSDN文档,并找到了一些不必要的描述和代码示例。我自己并没有尝试这个例子,但是我注意到了一个不同之处,该示例使用单文件代码(即
代码是在.aspx文件中编写的)而我编写了代码在
代码隐藏模式下。所以我想知道这是不是这个原因?但是,如果MS说DataItem属性不可用,我恐怕会感到困惑
通过代码隐藏:)
请帮忙吗?


感谢您的回复。这是代码片段:


private void Page_Load(object sender,System.EventArgs e)

{

// Put用户代码在这里初始化页面

testAdapter.Fill(dataSet);

dataGrid.DataSource = dataSet;

dataGrid.DataBind();


Response.Write(dataGrid.Items [0] .DataItem.ToString()); // A

将抛出NullReferenceException!

}


testAdapter执行SelectCommand作为''select * from Customers在Northwind数据库上打开

。实际上,从

数据库返回了几十行。但是,当我尝试使用

Response.Write()打印出DataItem.ToString()时,发生了NullReferenceException。但它工作正常,之后

我评论了这行代码。

我不知道我是否以正确的方式编写代码,请你< br $> b $ b指出我了?谢谢:)

" Eliyahu Goldin" <再************* @ monarchmed.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

有代码的位置没有区别。

从代码和.aspx文件中发布相关的片段,很可能那里有一个问题。

Eliyahu

Laser Lu < LA ****** @ hotmail.com>在消息中写道
新闻:u2 ************* @ TK2MSFTNGP15.phx.gbl ...

全部,
我是由DataGridItem.DataItem属性纠缠在一起。我试图调用
该属性来获取并显示绑定到当前所选行的内容。但是,一个例外让我感到惊讶,它说对象引用未设置为对象的实例。它怎么会发生?我已经设置了数据源,并调用了DataBind()。
我查找了MSDN文档,并找到了一些不必要的描述和代码示例。我自己并没有尝试这个例子,但是我注意到一个区别,这个例子使用单文件代码
(即代码是用.aspx文件编写的)我编写代码时在
代码隐藏模式下。所以我想知道这是不是这个原因?但是,如果MS说DataItem属性不可用,我恐怕会感到困惑
通过代码隐藏:)
请帮忙吗?



您是否设置了DataMember属性?你必须将它设置为数据集中的表的名称。


您在代码的哪一部分引用dataGrid.Items [0 ] .DataItem?

通常情况下,它应该是与项目相关的事件之一,例如

ItemDataBound或ItemCreated。


如果还不够,请发布.aspx文件的相关部分。不要发送

附件。


Eliyahu


" Laser Lu" < LA ****** @ hotmail.com>在消息中写道

news:e5 ************* @ TK2MSFTNGP14.phx.gbl ...

和.aspx文件和.cs文件在附件中提供,以方便您的使用。 :)

Laser Lu < LA ****** @ hotmail.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...

感谢您的回复。以下是代码片段:

private void Page_Load(object sender,System.EventArgs e)
//
//用户代码初始化页面
testAdapter .Fill(dataSet);
dataGrid.DataSource = dataSet;
dataGrid.DataBind();

Response.Write(dataGrid.Items [0] .DataItem.ToString() ); //这里将抛出NullReferenceException!

testAdapter在Northwind数据库上执行SelectCommand作为''来自客户'的选择'。实际上,从
数据库返回了几十行。但是,当我尝试使用Response.Write()打印出DataItem.ToString()
时,发生了NullReferenceException。但它工作正常,
之后我评论了这行代码。
我不知道我是否以正确的方式编写代码,请你
取悦<指出我?谢谢:)

" Eliyahu Goldin" <再************* @ monarchmed.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

其中没有区别代码是。

从代码和.aspx文件发布相关的片段,可能
那里有一个问题。

Eliyahu

Laser Lu < LA ****** @ hotmail.com>在消息中写道
新闻:u2 ************* @ TK2MSFTNGP15.phx.gbl ...
全部,
我被DataGridItem纠结了.DataItem属性。我试图
调用该属性来获取并显示绑定到当前所选行的内容。但是,一个例外让我感到惊讶,它说对象引用未设置为对象的实例。它怎么会发生?我已经设置了数据源,并调用了DataBind()。
我查找了MSDN文档,并找到了一些不必要的描述和代码示例。我自己并没有尝试这个例子,但是我注意到一个区别,这个例子使用单文件代码
(即代码是用.aspx文件编写的)我编写代码时在
代码隐藏模式下。所以我想知道这是不是这个原因?但是,如果MS说DataItem属性不可用,我恐怕会感到困惑
通过代码隐藏:)
请帮忙吗?




Hi, all,
I was tangled by the DataGridItem.DataItem property. I tried to invoke
that property to get and display the contents that was bound to the
currently selected row. However, an exception surprised me that it said
"Object reference not set to an instance of an object." How could it happen?
I have set the data source, and called the DataBind().
I looked up the MSDN documents, and just find some inessential
descriptions, and a code example. I didn''t try that example by myself, but I
noticed a difference that, the example uses the single-file code (i.e code
was written in the .aspx file) while I wrote my code in the code-behind
mode. So I wonder if this is the reason? But I''m afraid I would be puzzled
if MS says the DataItem property will not be available through code-behind:)
Any help, please?

解决方案

There is no difference where the code is.

Post the relevant fragment from the code and the .aspx file, likely there is
a problem over there.

Eliyahu

"Laser Lu" <la******@hotmail.com> wrote in message
news:u2*************@TK2MSFTNGP15.phx.gbl...

Hi, all,
I was tangled by the DataGridItem.DataItem property. I tried to invoke
that property to get and display the contents that was bound to the
currently selected row. However, an exception surprised me that it said
"Object reference not set to an instance of an object." How could it
happen? I have set the data source, and called the DataBind().
I looked up the MSDN documents, and just find some inessential
descriptions, and a code example. I didn''t try that example by myself, but
I noticed a difference that, the example uses the single-file code (i.e
code was written in the .aspx file) while I wrote my code in the
code-behind mode. So I wonder if this is the reason? But I''m afraid I
would be puzzled if MS says the DataItem property will not be available
through code-behind:)
Any help, please?



Thanks for your response. Here is the code fragment:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
testAdapter.Fill(dataSet);
dataGrid.DataSource = dataSet;
dataGrid.DataBind();

Response.Write(dataGrid.Items[0].DataItem.ToString()); // A
NullReferenceException will be thrown here!
}

The testAdapter executes an SelectCommand as ''select * from Customers'' on
the Northwind database. Actually, dozens of rows was returned from the
database. However, when I tried to print out the DataItem.ToString() using
Response.Write(), a NullReferenceException occured. But it works fine, after
I had commented that line of code.
I don''t know whether I wrote the code in the correct way, would you please
point me out? Thanks:)
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

There is no difference where the code is.

Post the relevant fragment from the code and the .aspx file, likely there
is a problem over there.

Eliyahu

"Laser Lu" <la******@hotmail.com> wrote in message
news:u2*************@TK2MSFTNGP15.phx.gbl...

Hi, all,
I was tangled by the DataGridItem.DataItem property. I tried to invoke
that property to get and display the contents that was bound to the
currently selected row. However, an exception surprised me that it said
"Object reference not set to an instance of an object." How could it
happen? I have set the data source, and called the DataBind().
I looked up the MSDN documents, and just find some inessential
descriptions, and a code example. I didn''t try that example by myself,
but I noticed a difference that, the example uses the single-file code
(i.e code was written in the .aspx file) while I wrote my code in the
code-behind mode. So I wonder if this is the reason? But I''m afraid I
would be puzzled if MS says the DataItem property will not be available
through code-behind:)
Any help, please?




Do you set DataMember property? You have to set it to the name of a table in
your dataset.

In what part of you code do you refer to dataGrid.Items[0].DataItem?
Typically, it should be in one of the item-related events, like
ItemDataBound or ItemCreated.

If it is not enough, post the relevant part of the .aspx file. Don''t sent
attachments.

Eliyahu

"Laser Lu" <la******@hotmail.com> wrote in message
news:e5*************@TK2MSFTNGP14.phx.gbl...

And the .aspx file and .cs file is provided in the attachment for your
convenience, if necessary. :)

"Laser Lu" <la******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

Thanks for your response. Here is the code fragment:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
testAdapter.Fill(dataSet);
dataGrid.DataSource = dataSet;
dataGrid.DataBind();

Response.Write(dataGrid.Items[0].DataItem.ToString()); // A
NullReferenceException will be thrown here!
}

The testAdapter executes an SelectCommand as ''select * from Customers'' on
the Northwind database. Actually, dozens of rows was returned from the
database. However, when I tried to print out the DataItem.ToString()
using
Response.Write(), a NullReferenceException occured. But it works fine,
after
I had commented that line of code.
I don''t know whether I wrote the code in the correct way, would you
please
point me out? Thanks:)
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

There is no difference where the code is.

Post the relevant fragment from the code and the .aspx file, likely
there
is a problem over there.

Eliyahu

"Laser Lu" <la******@hotmail.com> wrote in message
news:u2*************@TK2MSFTNGP15.phx.gbl...
Hi, all,
I was tangled by the DataGridItem.DataItem property. I tried to
invoke
that property to get and display the contents that was bound to the
currently selected row. However, an exception surprised me that it said
"Object reference not set to an instance of an object." How could it
happen? I have set the data source, and called the DataBind().
I looked up the MSDN documents, and just find some inessential
descriptions, and a code example. I didn''t try that example by myself,
but I noticed a difference that, the example uses the single-file code
(i.e code was written in the .aspx file) while I wrote my code in the
code-behind mode. So I wonder if this is the reason? But I''m afraid I
would be puzzled if MS says the DataItem property will not be available
through code-behind:)
Any help, please?





这篇关于DataGridItem.DataItem可用性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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