Row Item in DataView - OnRowDatabound event

E

Elmo Watson

I previously had a project working, in which the Gridview was populated by a
DataSet - then, with a DropDownlist in one of the columns, using the
OnrowDataBound event, to populate the DDL with the correct item in the
returned data, I had :
myDDL.SelectedValue =
IIf(IsDBNull(MyDataSet.Rows(e.Row.RowIndex).Item("Fieldname")),
vbNullString, MyDataSet.Rows(e.Row.RowIndex).Item("Fieldname"))

In my new application, I'm using a DataView to populate the Gridview, since
there are filters which need to be used, dynamically - - I tried the same
kind of thing, but I'm not sure how it would work with a DataView:

Here's what I have that doesn't work:
..SelectedValue = IIf(IsDBNull(myDV.Item(e.Row.RowIndex).Item("Fieldname")),
vbNullString, myDV.Item(e.Row.RowIndex).Item("Fieldname"))

myDVis saved in cache, and retrieved when needed. However, I get the dreaded
"Object Reference not set to an instance of the object' error when it gets
to that line)
(and yes, this all between an "If e.Row.RowType = DataControlRowType.DataRow
Then" block

Any ideas on how to get that to work?
 
A

Alvin Bruney [MVP]

This is really the old way of doing things. The new way would be to first
convert the gridview column that contains the dropdown to a template column,
add a dropdown control, add an sqldatasource, bind the sqldatasource to the
database to get the column field and then bind the dropdown to
sqldatasource. The benefit is zero code and zero event hookup.
Here's what I have that doesn't work:
.SelectedValue =
IIf(IsDBNull(myDV.Item(e.Row.RowIndex).Item("Fieldname")),
vbNullString, myDV.Item(e.Row.RowIndex).Item("Fieldname"))
I believe there is a bug here. You are using the row index to index into the
dataview items. There's no guarantee that the two will map to the same hence
the null object error. You'd be better served mapping to the respective row
of the dataview object instead of indexing into its item collection.
Something like myDV.Tables.Rows(e.Row.Rowindex should get you started on the
right path.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 
E

Elmo Watson

I agree with you to a point, but not all situations provide the choice for
that one scenario. As far as I know, the DataSource controls either provide
a DataSet or DataReader.
The problem is, that, at this point, it can't be re-constructed - this is
all object based, and the connection is made using an object, a DataView is
returned, so filters can be applied.
I tried myDV.Table.Rows(e.row.RowIndex)
however I get the same error.

Alvin Bruney said:
This is really the old way of doing things. The new way would be to first
convert the gridview column that contains the dropdown to a template column,
add a dropdown control, add an sqldatasource, bind the sqldatasource to the
database to get the column field and then bind the dropdown to
sqldatasource. The benefit is zero code and zero event hookup.
Here's what I have that doesn't work:
.SelectedValue =
IIf(IsDBNull(myDV.Item(e.Row.RowIndex).Item("Fieldname")),
vbNullString, myDV.Item(e.Row.RowIndex).Item("Fieldname"))
I believe there is a bug here. You are using the row index to index into the
dataview items. There's no guarantee that the two will map to the same hence
the null object error. You'd be better served mapping to the respective row
of the dataview object instead of indexing into its item collection.
Something like myDV.Tables.Rows(e.Row.Rowindex should get you started on the
right path.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------


Elmo Watson said:
I previously had a project working, in which the Gridview was populated by
a
DataSet - then, with a DropDownlist in one of the columns, using the
OnrowDataBound event, to populate the DDL with the correct item in the
returned data, I had :
myDDL.SelectedValue =
IIf(IsDBNull(MyDataSet.Rows(e.Row.RowIndex).Item("Fieldname")),
vbNullString, MyDataSet.Rows(e.Row.RowIndex).Item("Fieldname"))

In my new application, I'm using a DataView to populate the Gridview,
since
there are filters which need to be used, dynamically - - I tried the same
kind of thing, but I'm not sure how it would work with a DataView:

Here's what I have that doesn't work:
.SelectedValue =
IIf(IsDBNull(myDV.Item(e.Row.RowIndex).Item("Fieldname")),
vbNullString, myDV.Item(e.Row.RowIndex).Item("Fieldname"))

myDVis saved in cache, and retrieved when needed. However, I get the
dreaded
"Object Reference not set to an instance of the object' error when it gets
to that line)
(and yes, this all between an "If e.Row.RowType =
DataControlRowType.DataRow
Then" block

Any ideas on how to get that to work?
 
A

Alvin Bruney [MVP]

Couple things wrong in your code. I provide the correct syntax below -
you'll need to translate into vb. Additionally, the rowindex isn't valid for
the gridview header, it actually is set to -1. That is an invalid index in
the dataview. You'll need to trap for that condition to avoid the header
row. Here's working code.

DataView view = (DataView)Session["view"];

if (view == null)

{

Response.Write("<script>alert('empty')</script>");

}

else

{

if (e.Row.RowIndex > -1)

{

object[] items = view.Table.Rows[e.Row.RowIndex].ItemArray;

string str = items[3].ToString();

}

}


--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------


Elmo Watson said:
I agree with you to a point, but not all situations provide the choice for
that one scenario. As far as I know, the DataSource controls either
provide
a DataSet or DataReader.
The problem is, that, at this point, it can't be re-constructed - this is
all object based, and the connection is made using an object, a DataView
is
returned, so filters can be applied.
I tried myDV.Table.Rows(e.row.RowIndex)
however I get the same error.

Alvin Bruney said:
This is really the old way of doing things. The new way would be to first
convert the gridview column that contains the dropdown to a template column,
add a dropdown control, add an sqldatasource, bind the sqldatasource to the
database to get the column field and then bind the dropdown to
sqldatasource. The benefit is zero code and zero event hookup.
Here's what I have that doesn't work:
.SelectedValue =
IIf(IsDBNull(myDV.Item(e.Row.RowIndex).Item("Fieldname")),
vbNullString, myDV.Item(e.Row.RowIndex).Item("Fieldname"))
I believe there is a bug here. You are using the row index to index into the
dataview items. There's no guarantee that the two will map to the same hence
the null object error. You'd be better served mapping to the respective row
of the dataview object instead of indexing into its item collection.
Something like myDV.Tables.Rows(e.Row.Rowindex should get you started on the
right path.
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------


Elmo Watson said:
I previously had a project working, in which the Gridview was populated by
a
DataSet - then, with a DropDownlist in one of the columns, using the
OnrowDataBound event, to populate the DDL with the correct item in the
returned data, I had :
myDDL.SelectedValue =
IIf(IsDBNull(MyDataSet.Rows(e.Row.RowIndex).Item("Fieldname")),
vbNullString, MyDataSet.Rows(e.Row.RowIndex).Item("Fieldname"))

In my new application, I'm using a DataView to populate the Gridview,
since
there are filters which need to be used, dynamically - - I tried the same
kind of thing, but I'm not sure how it would work with a DataView:

Here's what I have that doesn't work:
.SelectedValue =
IIf(IsDBNull(myDV.Item(e.Row.RowIndex).Item("Fieldname")),
vbNullString, myDV.Item(e.Row.RowIndex).Item("Fieldname"))

myDVis saved in cache, and retrieved when needed. However, I get the
dreaded
"Object Reference not set to an instance of the object' error when it gets
to that line)
(and yes, this all between an "If e.Row.RowType =
DataControlRowType.DataRow
Then" block

Any ideas on how to get that to work?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top