如何将变量绑定到Web表单的gridview中 [英] How to bind a variable into a gridview for web forms

查看:54
本文介绍了如何将变量绑定到Web表单的gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我希望变量'v_total'与'gridview3'动态绑定。

以下是我的代码:



In my project I want the variable 'v_total' to bind dynamically with 'gridview3'.
Hereunder is my code:

</Columns>
       <RowStyle HorizontalAlign="Center" />
       <Columns>
       <asp:BoundField DataField="item_code" HeaderText="item_code"  />
</Columns>





protected void Button18_Click(object sender,EventArgs e)

{

v_total = v_total + 1;

}

protected void Button19_Click(object sender,EventArgs e)

{

SqlDataAdapter da1 = new SqlDataAdapter(select item_code from item,con);

SqlCommandBuilder cb21 = new SqlCommandBuilder(da1);

DataSet ds1 = new DataSet(member1) ;

da1.Fill(ds1,member1);

GridView3.DataSource = ds1;

GridView3.DataBind();

GridView3.Visible = true;

}



protected void Button18_Click(object sender, EventArgs e)
{
v_total = v_total + 1;
}
protected void Button19_Click(object sender, EventArgs e)
{
SqlDataAdapter da1 = new SqlDataAdapter("select item_code from item", con);
SqlCommandBuilder cb21 = new SqlCommandBuilder(da1);
DataSet ds1 = new DataSet("member1");
da1.Fill(ds1, "member1");
GridView3.DataSource = ds1;
GridView3.DataBind();
GridView3.Visible = true;
}

推荐答案

您好,



代码为上面描述的有点令人困惑。你有两个事件处理程序。第一个使用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.


这篇关于如何将变量绑定到Web表单的gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆