一列内容之外的网格视图问题 [英] grid view problem in addition of content of one column

查看:48
本文介绍了一列内容之外的网格视图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的网格视图就像


总计----余额
10 ----- 10
20 ----- 30
5------ 35


总列是通过某些条件从数据库中获取的,我想在运行时生成平衡列,如上述
任何人都可以向我解释biggener

解决方案

好吧,对于初学者来说,这是方向:
您需要使用gridview的RowDataBound方法.将要求将行数据绑定到网格.在此方法中,找到该行的合计值并添加到全局计数器.将当前总计分配给该行的余额字段.全局计数器将一直跟踪总和,直到该行绑定为您提供所需的余额总和.

现在试试.如有必要,请阅读RowDataBound并了解其工作原理.




在GridView的RowDataBound Event中添加列余额和下面的代码

 // 如果是绑定列
如果(例如,Row.RowType == DataControlRowType.DataRow)
{
    如果(例如,Row.RowIndex ==  0 )
    {
        e.Row.Cells [ 7 ].Text = e.Row.Cells [ 6 ].Text;
    }
    其他
    {
        e.Row.Cells [ 7 ].Text = Convert.ToInt32(GridView1.Rows [e.Row.RowIndex- 1 ].Cells [ 7 ].Text)+ Convert.ToInt32(e.Row.Cells [ 6 ].Text);
    }

}

// 如果是模板列

标签lbl = e.Row.FindControl(" )标签;
如果(例如,Row.RowType == DataControlRowType.DataRow)
{
    如果(例如,Row.RowIndex ==  0 )
    {
        lbl.Text = e.Row.Cells [ 6 ].Text;
    }
    其他
    {
        lbl = Convert.ToInt32((((Label)GridView1.Rows [e.Row.RowIndex- 1 ] .. FindControl("  lblBalance")).Text)+ Convert.ToInt32(e.Row.Cells [ <   asp:gridview     id   ="     runat   =" 服务器" <   > 
       <   asp:templatecolumn    标题  ="  <   itemtemplate  > 
                  <   asp:label     id   ="   runat   服务器" 文本  " / <  /itemtemplate  > 
       <  /asp:templatecolumn  > 
   <  /列 > 
<  /asp:gridview  >  



在解决方案2中检查我更新的模板栏答案.


hi
my grid view is like


total----balance
10 ----- 10
20 ----- 30
5 - ----- 35


the total column is fetched from data base by some conditions and i want to generate balance column at run time as above
any one can explain i m just biggener

解决方案

Ok, for a beginner, here is the direction:
You need to use gridview''s RowDataBound method. This will be called for row databind to grid. In this method, find the value of total for that row and add to a global counter. Assign the current total to the balance field of that row. Global counter will keep track of sum till that row bind giving you balance sum as you need.

Try now. If needed, read about RowDataBound and see how it works.


hi,

Add the column balance and below code in RowDataBound Event of the GridView

//if bound column
if(e.Row.RowType==DataControlRowType.DataRow)
{
    if(e.Row.RowIndex==0)
    {
        e.Row.Cells[7].Text=e.Row.Cells[6].Text;
    }
    else
    {
        e.Row.Cells[7].Text = Convert.ToInt32(GridView1.Rows[e.Row.RowIndex - 1].Cells[7].Text) + Convert.ToInt32( e.Row.Cells[6].Text);
    }

}

//if template column

Label lbl = e.Row.FindControl("lblBalance") as Label;
if(e.Row.RowType==DataControlRowType.DataRow)
{
    if(e.Row.RowIndex==0)
    {
        lbl.Text=e.Row.Cells[6].Text;
    }
    else
    {
        lbl= Convert.ToInt32(((Label)GridView1.Rows[e.Row.RowIndex - 1].FindControl("lblBalance")).Text) + Convert.ToInt32( e.Row.Cells[6].Text);
    }

}



hope it helps.


add the template column in the gridview source along with other columns. take care about the column indexes.

<asp:gridview id="GridView1" runat="server">
   <columns>
       <asp:templatecolumn headertext="Balance">
             <itemtemplate>
                  <asp:label id="lblBalance" runat="server" text="" />
             </itemtemplate>
       </asp:templatecolumn>
   </columns>
</asp:gridview>



check my updated answer for template column in Solution 2.


这篇关于一列内容之外的网格视图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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