Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
[no name] 22-Feb-12 4:22am    
can you paste the code!!!!11
for more detail
klps11 22-Feb-12 4:31am    
it should be like this

<pre>
protected void RowDBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(type == 1)
{
e.Row.Cells[2].Text = (ds.Tables[0].Rows[0]["amount"]).ToString();
}
if(type == 2)
{
e.Row.Cells[3].Text = (ds.Tables[0].Rows[0]["amount"]).ToString();
}
}
}
</pre>


but when I add the second record, it always add the amount to all the cell[2] or cell[3] of all rows. I need to add the new row while the older ones are there as it is

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[^]
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
Varun Sareen 22-Feb-12 4:38am    
For hiding or showing the columns at respective times, please refer to the solution by CRDave
klps11 23-Feb-12 2:16am    
if it is not a lable, how should this line changed??

Label lblIncome=(Label)e.Row.FindControl("lblIncome");
Varun Sareen 23-Feb-12 11:45am    
say it is text box then do it like this:-

TextBox txtIncome=(TextBox )e.Row.FindControl("txtIncome");

you just have to use the control type like TextBox, LinkButton, Lable etc.
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();
                }
                
   
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900