Click here to Skip to main content
Rate this: bad
good
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">
    <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:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        EquipCost();  //Loads gridview from Datatable.

    }
}
Posted 29-Jan-13 5:49am
Comments
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.
Try: http://www.codeproject.com/Articles/384425/Server-side-Delimiters-in-ASP-NET
FabeCode at 29-Jan-13 12:57pm
   
Sorry you are correct.
Rate this: bad
good
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]".
  Permalink  
Rate this: bad
good
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)
  Permalink  
Comments
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)



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
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