Click here to Skip to main content
11,714,345 members (83,026 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Gridview
I am unable to determine the "SelectedRow" and consequently the cell values needed to calculate the "Total" column of the gridview.

I populate the gridview from a datatable created from a "SqlDataReader". It populates appropriately, but I get "gvEquipment.SelectedRow" = -1 and gvEquipment.Rows[4].Cells[3].Text= "". (All of the .TEXT values are blank.)

<asp:GridView ID="gvEquipment" runat="server" AutoGenerateColumns="False" EnableModelValidation="True"
     Height="150px" Style="margin-right: 1px">
        <asp:TemplateField HeaderText="Equipment ID" Visible="False">
                <asp:TextBox ID="txtEquipID" runat="server" ReadOnly="True" Text='<%# DataBinder.Eval(Container.DataItem, "Equip ID") %>'
                    Width="<%# 30 %>"></asp:TextBox>
            <HeaderStyle Width="125px" />
        <asp:TemplateField HeaderText="Total #">
                <asp:TextBox ID="txtEquipCnt" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Total #") %>'
                    Width="<%# 25 %>"
                    AutoPostBack="True" ontextchanged="txtEquipCnt_TextChanged"></asp:TextBox>
            <HeaderStyle Width="40px" />
        <asp:TemplateField HeaderText="Equipment">
                <asp:TextBox ID="txtEquipDesc" runat="server" ReadOnly="True" Text='<%# DataBinder.Eval(Container.DataItem, "EquipDesc") %>'
                    Width="<%# 110 %>"></asp:TextBox>
            <HeaderStyle Width="130px" />
        <asp:TemplateField HeaderText="Cost/Hour">
                <asp:TextBox ID="txtEquipCharge" runat="server" ReadOnly="True" Text='<%#String.Format("{0, 0:N2}", DataBinder.Eval(Container.DataItem, "CostPerHour")) %>'
                    Width="<%# 55 %>" ></asp:TextBox>
            <HeaderStyle Width="40px" />
        <asp:TemplateField HeaderText="Total Cost">
                <asp:TextBox ID="txtEquipCost" runat="server" ReadOnly="True" Text='<%# String.Format("{0:0,0.00}", DataBinder.Eval(Container.DataItem, "EquipCost")) %>'
                    Width="<%# 75 %>"></asp:TextBox>
            <HeaderStyle Width="100px" />

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        EquipCost();  //Loads gridview from Datatable.

Posted 29-Jan-13 5:49am
Sandeep Mewara at 29-Jan-13 11:59am
What's this and why?
Width="<%# 30 %>"
FabeCode at 29-Jan-13 12:03pm
It formats the size of the element within the column.
Sandeep Mewara at 29-Jan-13 12:25pm
Why not directly '30'?
FabeCode at 29-Jan-13 12:48pm
Percent (%) keeps it relative to the total column width. So if the total column width changes, the object's size will automatically. You won't have to manually change it.
Sandeep Mewara at 29-Jan-13 12:55pm
You sure that above code does a 30% and not 30? I doubt.
FabeCode at 29-Jan-13 12:57pm
Sorry you are correct.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Because of the control within the cell, there is another layer which must be evaluated. The following retrieves the contents of the cell:

String t1 = ((TextBox)(gvAssgnSmry.Rows[0].Cells[1].Controls[1])).Text;

(It's a different gridview (gvAssgnSumry vs gvEquipment, but the principal is the same)

I did not have the "Controls[1]".
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

DataRowView drv = (Name of DataGrid).CurrentCell.Item as DataRowView;

You should be able to attach the line of code to the event handler of your choice.

Then, reference individual values in this manner:

drv.Row[0] (Zero indicates which column you want to select)
FabeCode at 29-Jan-13 12:13pm
I placed "DataRowView drv = gvEquipment.CurrentCell.Item as DataRowView;" in the "protected void txtEquipCnt_TextChanged(object sender, EventArgs e)" method.

I get the following error:

'System.Web.UI.WebControls.GridView' does not contain a definition for 'CurrentCell' and no extension method 'CurrentCell' accepting a first argument of type 'System.Web.UI.WebControls.GridView' could be found (are you missing a using directive or an assembly reference?)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 540
1 Maciej Los 259
2 Richard MacCutchan 230
3 OriginalGriff 140
4 F-ES Sitecore 140
0 Sergey Alexandrovich Kryukov 545
1 CHill60 375
2 Maciej Los 324
3 Richard MacCutchan 310
4 OriginalGriff 260

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100