Retrieve Column Value from Data Grid

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

How can I retrieve a column value from a Gridview? I've attached the
SQL String form the Sqldatasource. I'm trying to retrieve StepDate.
The columns in the Gridview are in the same order.

SELECT CONVERT (varchar(10), StepDate, 101) AS StepDate, Steps FROM
tblSteps WHERE (DomainName = @DomainName) ORDER BY StepDate DESC

Thanks
 
Dave said:
How can I retrieve a column value from a Gridview? I've attached the
SQL String form the Sqldatasource. I'm trying to retrieve StepDate.
The columns in the Gridview are in the same order.

SELECT CONVERT (varchar(10), StepDate, 101) AS StepDate, Steps FROM
tblSteps WHERE (DomainName = @DomainName) ORDER BY StepDate DESC

Thanks

Assuming that you are using an ASP.NET gridview (I'm not sure that my
response fit also windows.forms grid)
it depends on the place where you want to read the value.
If you are on Row_DataBound event you can get values from EventArgs object
(usually "e") in many ways.
I suggest you to get the object bound to the gridview instead of get values
directly from grid.

If you have a grid bound to custom objects you can get the dataobject with :

MyObject myobj = (MyObject)e.Row.DataItem;

or else if you have the grid bound with a dataset/datatable you can get the
DataRow object that contains all fields arriving from db.

DataRow myRow = (DataRow)e.Row.DataItem;

As last method you can finally use :

e.Row.Cells[0].Text

to get the value in the firs column. (0 is column index).
If you are out of grid event (like Row_DataBound) you must iterates items or
declare exact coordinates :

myGrid.Rows[0].Cells[0]

or else get the object bound to the grid :

myGrid.Rows[0].DataItem



Bob
 
Bob,
thanks for taking an interest in my posting. Yes, it is a GridView
control (GridView1) in ASP.Net. I know...I need to work on my Naming
Convention. It's bound to a SQLDatasource that only returns 2
columns. StepDate and Steps. I'm interested in pulling the StepDate
when a record is selected. So far the only luck I've had where the
page doesn't error out only returns
System.Web.UI.WebControls.DataControlFieldCell.

Thanks again for responding.











Dave said:
How can I retrieve a column value from a Gridview? I've attached the
SQL String form the Sqldatasource. I'm trying to retrieve StepDate.
The columns in the Gridview are in the same order.
SELECT CONVERT (varchar(10), StepDate, 101) AS StepDate, Steps FROM
tblSteps WHERE (DomainName = @DomainName) ORDER BY StepDate DESC

Assuming that you are using an ASP.NET gridview (I'm not sure that my
response fit also windows.forms grid)
it depends on the place where you want to read the value.
If you are on Row_DataBound event you can get values from EventArgs object
(usually "e") in many ways.
I suggest you to get the object bound to the gridview instead of get values
directly from grid.

If you have a grid bound to custom objects you can get the dataobject with :

MyObject myobj = (MyObject)e.Row.DataItem;

or else if you have the grid bound with a dataset/datatable you can get the
DataRow object that contains all fields arriving from db.

DataRow myRow = (DataRow)e.Row.DataItem;

As last method you can finally use :

e.Row.Cells[0].Text

to get the value in the firs column. (0 is column index).
If you are out of grid event (like Row_DataBound) you must iterates items or
declare exact coordinates :

myGrid.Rows[0].Cells[0]

or else get the object bound to the grid :

myGrid.Rows[0].DataItem

Bob
 
Dave said:
Bob,
thanks for taking an interest in my posting. Yes, it is a GridView
control (GridView1) in ASP.Net. I know...I need to work on my Naming
Convention. It's bound to a SQLDatasource that only returns 2
columns. StepDate and Steps. I'm interested in pulling the StepDate
when a record is selected. So far the only luck I've had where the
page doesn't error out only returns
System.Web.UI.WebControls.DataControlFieldCell.

Thanks again for responding.

Hi Dave,
there many ways to manage gridview and the best method depends on your
needs.
If I suppose right you are using events SelectedIndexChanged or
SelectedIndexChanging to get selected rows.
In this case you can do something like this (use the event depending on your
needs) :

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string myField= GridView1.SelectedRow.Cells[1].Text;
}


protected void GridView1_SelectedIndexChanging(object sender,
GridViewSelectEventArgs e)
{
string myField = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
}

The "e" argument changes depending on the events where you are.

For more details check out microsoft.public.dotnet.framework.aspnet
newsgroup.
I'm new in this ng but I think it's related to language features and
implementations, not focused on aspnet handling :)

For gridview check also this link :
http://www.codersource.net/asp_net_grid_view_whidbey.aspx


Good work :)
Bob
 
Back
Top