Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.)


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



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

    }
}
Posted
Comments
Sandeep Mewara 29-Jan-13 11:59am    
What's this and why?
Width="<%# 30 %>"
FabeCode 29-Jan-13 12:03pm    
It formats the size of the element within the column.
Sandeep Mewara 29-Jan-13 12:25pm    
Why not directly '30'?
FabeCode 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 29-Jan-13 12:55pm    
You sure that above code does a 30% and not 30? I doubt.
Try: http://www.codeproject.com/Articles/384425/Server-side-Delimiters-in-ASP-NET

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]".
 
Share this answer
 
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)
 
Share this answer
 
Comments
FabeCode 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900