本文介绍了确定Gridview单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我无法确定SelectedRow,因此无法确定计算gridviewTotal列所需的单元格值。 我从一个从SqlDataReader创建的数据表中填充gridview。它适当地填充,但我得到gvEquipment.SelectedRow= -1和gvEquipment.Rows [4] .Cells [3] .Text =。 (所有.TEXT值都是空白。) < asp:GridView ID = gvEquipment runat = server AutoGenerateColumns = False EnableModelValidation = True 高度 = 150px 样式 = margin-right:1px > < 列 > < asp:TemplateField HeaderText = 设备ID 可见 = False > < ItemTemplate > < asp:TextBox ID = txtEquipID runat = server ReadOnly = True 文本 =' <% #DataBinder.Eval(Container.DataItem, 装备ID)%> ' 宽度 = <%# 30 %> > < / asp:TextBox > < / ItemTemplate > < HeaderStyle 宽度 = 125px / > < / asp:TemplateField > < asp:TemplateField HeaderText = 总计# > < ItemTemplate > < asp:TextBox ID = txtEquipCnt runat = server 文字 =' <% #DataBinder.Eval(Container.DataItem, Total#)%> ' 宽度 = <%# 25 %> AutoPostBack = True ontextchanged = txtEquipCnt_TextChanged > < / asp:TextBox > < / ItemTemplate > < HeaderStyle 宽度 = 40px / > < / asp:TemplateField > < asp:TemplateField HeaderText = 设备 > < ItemTemplate > < asp:TextBox ID = txtEquipDesc runat = server ReadOnly = True 文字 =' <%#DataBinder.Eval(Container.DataItem, EquipDesc)%> ' 宽度 = <%# 110 %> > < / asp:TextBox > < / ItemTemplate > < HeaderStyle 宽度 = 130px / > < / asp:TemplateField > < asp:TemplateField HeaderText = 费用/小时 > < ItemTemplate > < asp:TextBox ID = txtEquipCharge runat = 服务器 ReadOnly = True 文字 =' <%#String.Format( {0,0:N2},DataBinder.Eval(Container.DataItem, CostPerHour))% > ' 宽度 = <%# 55 %> > < / asp:TextBox > < / ItemTemplate > < HeaderStyle 宽度 = 40px / > < / asp:TemplateField > < asp:TemplateField HeaderText = 总费用 > < ItemTemplate > < asp:TextBox ID = txtEquipCost runat = server ReadOnly = True 文字 =' <%#字符串 .Format( {0:0,0.00},DataBinder.Eval(Container.DataItem, EquipCost))%> ' 宽度 = <%# 75 %> ; > < / asp:TextBox > < / ItemTemplate > < HeaderStyle 宽度 = 100px / > < / asp:TemplateField > < /列 > < / asp:GridView > .cs: protected void Page_Load( object sender,EventArgs e) { if (!IsPostBack) { EquipCost(); // 从Datatable加载gridview。 } } 解决方案 由于单元格内的控制,还有另一层必须进行评估。以下检索单元格的内容: 字符串t1 =((TextBox)(gvAssgnSmry.Rows [0] .Cells [1] .Controls [1]) )。文字; (这是一个不同的gridview(gvAssgnSumry vs gvEquipment,但校长是一样的) 我没有 控件[1] DataRowView的DRV =(数据网格的名称)作为.CurrentCell.Item DataRowView的; 您应该能够将代码行附加到您选择的事件处理程序。 然后,引用此中的各个值方式: drv.Row [0](零表示你要选择哪一列) 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. }} 解决方案 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]".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) 这篇关于确定Gridview单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 09:19