如何在不同条件下将值绑定到两列 [英] How to bind a value to two columns in different conditions

查看:63
本文介绍了如何在不同条件下将值绑定到两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,当我向表中添加数据时,该应用程序需要将数据绑定到gridview. Gridview具有三列,包括收入"和费用".根据我给出的类型",应用程序应将数据(金额)绑定到收入或费用.

如果类型为1,则应将其绑定到收入列;如果类型为2,则应将其绑定到费用列.每次我添加数据时,都应更新gridview.我做了很多尝试,但是我还是编程新手.谁能帮我.

我试图在gridview的RowDataBound事件中做到这一点.

I''m having a web application which needs to bind data to a gridview when I add data to the table. Gridview has three columns including "income" and "expense". According to the "type" I''m giving, application should bind data(amount) either to income or expense.

if the type is 1 then it should bind it to income column and if the type is 2 it should bind it to expense column. each time when I''m adding data the gridview should be updated. I tried a lot to do this and I''m new to programming. can anyone help me.

I tried to do it in RowDataBound event of gridview.

推荐答案

同时包括两列,并根据您的需要(即您的类型)显示/隐藏列.

要了解如何隐藏/显示,请参考:

如何以编程方式隐藏Gridview列? [在ASP.NET中显示/隐藏GridView列 [ ^ ]
Include both column and do show/hide column according to your need (that is ur type).

To know how to hide/show refer this:

How to hide Gridview column programmatically?[^]

Show / Hide GridView Columns in ASP.NET[^]


尝试:-

您需要根据条件绑定数据,因此在这种情况下,您必须将数据绑定写入gridrowdatabound事件,即

try this:-

you need to bind you data according to condition so in that case you have to write the data bind into the gridrowdatabound event i.e.,

private void [gridName]_RowDataBound(Object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
{
 //lets say your columns are label and the value on the basis of which you want to bind the data into two columns is stored in hidden field then
Label lblIncome=(Label)e.Row.FindControl("lblIncome");
Label lblExpense=(Label)e.Row.FindControl("lblExpense");
HiddenField hdnType=(HiddenField)e.Row.FindControl("hdnType");

//Now we will check the condition on the basis on which you will bind data to income column or expense column
if(hdnType.Value=="1")
{
 lblIncome.Text=[Data fetched from database for income]
}
else if(hdnType.Value=="1")
{
 lblExpense.Text=[Data fetched from database for expense]
}
}
}



将类型字段绑定到隐藏字段,如下所示:-



Bind the type field to hidden field as:-

<asp:hiddenfield id="hdnType" runat="Server" value="<%#Eval("Type") %>" xmlns:asp="#unknown" />



如果有帮助,请不要忘记将其标记为答案.



Please don''t forget to mark this as your answer if it helps you out.


protected void RowDBound(object sender, GridViewRowEventArgs e)
        {
            int rowIndex = e.Row.RowIndex;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                DataRow dr = drv.Row;

                if (dr["type"].ToString() == "1")
                {
                    e.Row.Cells[2].Text = dr["amount"].ToString();
                }
                else if (dr["type"].ToString() == "2")
                {
                    e.Row.Cells[3].Text = dr["amount"].ToString();
                }
                
   
            }
        }


这篇关于如何在不同条件下将值绑定到两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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