locked
Update columns of a datatable RRS feed

  • Question

  • User-1382746017 posted

    I have a datatable with data as below;

    PID       TName        RName       NameID     PStatus      Status

    1           AA               AA                               1

    2           BB               DE                                0

    Now I want to update the datacolumns  "NameID" and "Status" for all the rows based on some conditions.

    To update NameID:- If columns TName and RName are same, the column NameID should be updated as "Success" else "Failure"

    To update Status:- If column "PStatus" has value 1, then Status column should be updated as "yes", and "No" for value 0

    So, the after updations, the datatable should be like below:

    PID       TName        RName       NameID     PStatus      Status

    1           AA               AA           Success          1              Yes

    2           BB               DE            Failure           0               No

    How to achieve this?

    Wednesday, August 26, 2015 6:21 AM

Answers

  • User1648350404 posted

    Try following:

    for(int i = 0; i < yourDataTable.Rows.Count; i++)
    {
     if (yourDataTable.Rows[i]["TName"].ToString() ==yourDataTable.Rows[i]["RName"].ToString() ){
     yourDataTable.Rows[i]["NameID"] = "Success";
    }
    else{
     yourDataTable.Rows[i]["NameID"] = "Failure";
    }
        if (yourDataTable.Rows[i]["PStatus"].ToString() == "1")
        { yourDataTable.Rows[i]["Status"] = "Yes"; }
        else
        { yourDataTable.Rows[i]["Status"] = "No"; }
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2015 7:31 AM

All replies

  • User1648350404 posted

    Try following:

    for(int i = 0; i < yourDataTable.Rows.Count; i++)
    {
     if (yourDataTable.Rows[i]["TName"].ToString() ==yourDataTable.Rows[i]["RName"].ToString() ){
     yourDataTable.Rows[i]["NameID"] = "Success";
    }
    else{
     yourDataTable.Rows[i]["NameID"] = "Failure";
    }
        if (yourDataTable.Rows[i]["PStatus"].ToString() == "1")
        { yourDataTable.Rows[i]["Status"] = "Yes"; }
        else
        { yourDataTable.Rows[i]["Status"] = "No"; }
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2015 7:31 AM
  • User61956409 posted

    Hi Osceria,

    To update NameID:- If columns TName and RName are same, the column NameID should be updated as "Success" else "Failure"

    To update Status:- If column "PStatus" has value 1, then Status column should be updated as "yes", and "No" for value 0

    According to your description, I create the following sample for your reference.

    <div>
        <asp:Button ID="btnbind" runat="server" Text="BindGridView" OnClick="btnbind_Click" />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    
    protected void btnbind_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PID");
        dt.Columns.Add("TName");
        dt.Columns.Add("RName");
        dt.Columns.Add("NameID");
        dt.Columns.Add("PStatus");
        dt.Columns.Add("Status");
    
        dt.Rows.Add("1", "AA", "AA", "", "1", "");
        dt.Rows.Add("2", "BB", "DE", "", "0", "");
    
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["TName"] == dt.Rows[i]["RName"])
            {
                dt.Rows[i]["NameID"] = "Success";
            }
            else
            {
                dt.Rows[i]["NameID"] = "Failure";
            }
    
            if (dt.Rows[i]["PStatus"].ToString() == "1") 
            {
                dt.Rows[i]["Status"] = "Yes";
            }
            else
            {
                dt.Rows[i]["Status"] = "No";
            }               
        }
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    
    }
    

    Best Regards,

    Fei Han



    Thursday, August 27, 2015 2:15 AM