Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:08:31+00:00 2026-06-12T01:08:31+00:00

I am using a gridview to display on a buttonClick in asp.net using c#.

  • 0

I am using a gridview to display on a buttonClick in asp.net using c#.

 DataSet dsnew  = new businessLogic.Biz().getData();
 if (dsnew != null && dsnew.Tables[0].Rows.Count > 0)
 {
     DataView myDataView = new DataView();
     myDataView = dsnew.Tables[0].DefaultView;
     grdDetail.DataSource = myDataView;
     grdDetail.DataBind();
 }

and on a rowDataBound, depending on the condition, i want to change the dataitem container value. I am doing it in the following way but the issue is that all the rows have the paymentmethod of the last row in the dataset.

<asp:TemplateField ItemStyle-Width="70" ItemStyle-HorizontalAlign="Center">
   <ItemTemplate>
       <asp:Label ID="lblPaymentMethod" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SellingPaymentMethod") %>'></asp:Label>
   </ItemTemplate>
</asp:TemplateField>


protected void grdDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        return;
    }

    foreach (DataRow dtrCurrentRow in (((System.Data.DataView)grdDetail.DataSource)).Table.Rows)
    {
        //DataRow row = (DataRow)e.Row.DataItem;
        Label lblPaymentMethod = e.Row.FindControl("lblPaymentMethod") as Label;

        if (Condition1))
        {
            lblPaymentMethod.Text = dtrCurrentRow["SellingPaymentMethod"].ToString();
        }
        else if (Condition 2)
        {
            lblPaymentMethod.Text = dtrCurrentRow["DeliveryPaymentmethod"].ToString();
        }         

    }    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:08:32+00:00Added an answer on June 12, 2026 at 1:08 am

    Instead of using rowDataBound event to check what value to display you may try to directly control visibility or directly set text property of your columns! I did a small example for you, which could be easily adapted to your needs!

    ASPX

    <asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Visible='<%# CheckCondition1(Eval("ID")) %>' Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ProductName">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Visible='<%# CheckCondition2(Eval("ProductName")) %>' Text='<%# Bind("ProductName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="thirdColumn">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# GetValue(Eval("ID")) %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
                <asp:TemplateField HeaderText="forthColumn">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# GetValue(Eval("ID"), "staticValue", Eval("ProductName")) %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    CodeBehind

    public bool CheckCondition1(object ID)
    {
        return ID.ToString() != "2";
    }
    public bool CheckCondition2(object ProductName)
    {
        return ProductName.ToString() != "jklö";
    }
    public string GetValue(object ID)
    {
        // check condition here       
        //    ...
        // and return according value
        return ID.ToString() + " is your ID";
    
        // eg
        //if (Condition1))
        //    return dtrCurrentRow["SellingPaymentMethod"].ToString();
        //else if (Condition 2)
        //    return dtrCurrentRow["DeliveryPaymentmethod"].ToString();
    }
    public string GetValue(object ID, object firstValue, object secondValue)
    {      
        if (ID.ToString() == "2")
            return firstValue.ToString();
        else
            return secondValue.ToString();
    }
    

    CodeBehind to show demo values

    // assume there is a class Products with id and ProductName
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            List<Product> products = new List<Product>();
            products.Add(new Product() { ID = 1, ProductName = "asdf" });
            products.Add(new Product() { ID = 2, ProductName = "asdf" });
            products.Add(new Product() { ID = 3, ProductName = "jklö" });
            products.Add(new Product() { ID = 4, ProductName = "asdf" });
            gvProducts.DataSource = products;
            gvProducts.DataBind();
        }
    }
    

    Should result in this:.

    expected result for the given code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a GridView control in ASP.NET web form to display the data
I'm using a GridView to display some ifnromation in ASP.NET. I need it to
I am using ASP.Net 2.0 and trying to display transformed xml data using GridView
I am using gridview in my ASP.NET web application and using templatefield to show
im using a gridview to display data taken from a dataset which looks like
i am developing one asp.net application,i am using one gridview and bind the data
I am creating a web app using asp.net and c#. I am using gridview
I am trying to display data from XML using dataset in gridview. Now I
I using a GridView to display a feed. If the entry has a background
I have a ListView which is using a GridView to display a DataTable and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.