views:

444

answers:

2

I have a datagrid that I need to get a value out of after it has been populated. I'd prefer it not be dynamic (no clicking). The datagrids final product is a set of headers and one row

Header1| Header2| Header3
Value1 | Value1 | Value1

I'd like to be able to get any value regardless of its position but right now I only need to get the last value from column 3.

I'm using C# and ASP.NET 3.5

My asp.net looks like this:

<asp:Panel runat="server" ID="pnlCheckData" Width="730px">     
    <asp:GridView DataKeyNames="uniqueName" id="ViewData" DataSourceID="srcView" Runat="server" AllowSorting="false" CellPadding="1" CellSpacing="1" Visible="true" GridLines="Vertical" Font-Size="X-Small">
        <AlternatingRowStyle CssClass="alternating" />         
    </asp:GridView>
</asp:Panel>


<asp:ObjectDataSource id="srcCertificationView" TypeName="CertificationClass" SelectMethod = "GetReportSummaryData" Runat="server">
    <SelectParameters>
        <asp:ControlParameter Name="datasourceID" ControlID="Selection" />
        <asp:ControlParameter Name="Code" ControlID="countrySelection" />
        <asp:ControlParameter Name="dateRange" ControlID="dateSelection" />
        <asp:ControlParameter Name="dateProcessed" ControlID="MostRecent" />
    </SelectParameters>
</asp:ObjectDataSource>
+1  A: 

Excuse my lack of proper syntax, I don't have visual studio or any C# code in front of me at the moment.

But using the ViewData, gridview object, you should be able to do something like

ViewData.getRows()

which will return a collection of rows you can iterate through.

When you get to the last row, you should be able to do

cells = row.getCells()
cells[3]

And go to the third cell.

James McMahon
Most of the examples I have found say I should use row/Rows but I always get a compile error: 'System.Web.UI.WebControls.ObjectDataSource' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'System.Web.UI.WebControls.ObjectDataSource'
aggitan
+2  A: 

Instead of letting the GridView auto-generate your columns, you may want to create your own column templates. This way you would be able to give the text boxes or labels it generates IDs such that you could use FindControl('controlID') to get the value from.

Another way to do it, if you really only need the last value from the third column, would be to do what nemo suggested (this code would be a little different if you had a footer row, but the following will get you the cell containing the data you want):

TableCell myTableCell = myGridView.Rows[myGridView.Rows.Count - 1].Cells[2];

You can then get myTableCell.Text or if, for example, there was a TextBox control in it: (myTableCell.Controls[0] as TextBox).Text

Hope this helps.

Cory Larson