且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将变量绑定到Web表单的gridview中

更新时间:2022-04-26 02:34:43

您好,



代码为上面描述的有点令人困惑。你有两个事件处理程序。第一个使用1增加变量 v_total ,而第二个使用数据库中的SQL查询填充数据集。然后,将该数据集作为数据源绑定到gridview。我缺少的是你想在gridview中使用 v_total 的地方。您只能将一个数据源绑定到gridview,因此您必须将值直接绑定到列或单元格。例如,如果你想将该值绑定到gridview中的特定标签,你可以尝试这样的事情:



Hello,

The code as described above is slightly confusing. You have two event handlers. The first increases the variable v_total with 1, while the second fills a dataset with an SQL query from a database. Then, you bind that dataset as a datasource to the gridview. The bit that I'm missing is where you want to use the v_total in your gridview. You can only bind one datasource to your gridview so you'll have to bind your value directly to a column or cell. If you want to , for example, bind that value to a particular label in your gridview, you could try something like this :

<asp:GridView ID="gridProgress" runat="server" AutoGenerateColumns="False">
    <Columns>
       <asp:TemplateField HeaderText="Total Value">
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
          <HeaderStyle HorizontalAlign="Center" />
          <ItemTemplate>
              <asp:Label ID="lblTotalValue" runat="server"></asp:Label>
          </ItemTemplate>
      </asp:TemplateField>
    </Columns>
</asp:GridView>



使用Gridview的FindControl属性:


Use the FindControl property of Gridview :

Label lbl = GridView3.Rows[rowIndex].FindControl("lblTotalValue") as Label;
lbl.Text = Convert.ToString(v_total);



当然,您也可以将您的值绑定到gridview中的其他控件。您还可以将值 v_total 添加到数据集(或数据表(也可以作为数据源工作)),然后将其直接绑定到gridview。希望这会有所帮助。


Of course, you could bind your value to other controls inside your gridview as well. You could also add your value v_total to the dataset ( or datatable (works cool as datasource as well)) and then bind it directly to the gridview. Hope this helps.