简体   繁体   中英

Change GridView Column Value in C#

I have a GridView which has one column Status, which has default value False(in a label) in each Row. There is one Column named as Student, this is link button. So when User clicks on Student name
a gridview opens for to display data for that User. I change data for second GridView and then save it. So When this data is successfully saved. I want to modify the Status of 1st GridView from False to True for that particular row. So please suggest me how to do it? Is it necessary to bind the GridView again. IS there any easy method, please let me know.

So the main problem is how to modify a particular cell value in a GridView, based on some some action.

I'm writing the piece of Code, which I'm trying. I 'm saving the Parent GridView Row no. is ViewState.

protected void btnSave_Click(object sender, EventArgs e)
{
    if (ViewState["RowIndexPOS"] != null)
    {
        int iRowIndex = Convert.ToInt32(ViewState["RowIndexPOS"]);
        Label lblStatus = (Label)GridViewMultiplePOSAssociationId.Rows[iRowIndex].FindControl("LabelStatusPendingPOSId");
        //Means all rows in GridView are successfully associated
        if (table.Rows.Count == iResultCount)
        {
            lblStatus.Text = "Associated";
        }
        else
        {
            lblStatus.Text = "Pending";
        }
    }
}

Code to bind first gridview

DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Row Number", typeof(string)));
dt.Columns.Add(new DataColumn("POS Id", typeof(string)));
dt.Columns.Add(new DataColumn("Action", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
for (int index = 0; index < m_listStrPendingListOfPOS.Count; index++)
{
    dr = dt.NewRow();
    int iRowNo = index + 1;
    dr["Row Number"] = iRowNo;
    string strGridViewPOSId = m_listStrPendingListOfPOS[index];
    dr["POS Id"] = strGridViewPOSId;
    dr["Action"] = string.Empty;
    dr["Status"] = "Pending";
    dt.Rows.Add(dr);                                            
}

ViewState["POSTable"] = dt;
GridViewMultiplePOSAssociationId.DataSource = dt;
GridViewMultiplePOSAssociationId.DataBind();  

When I'm trying this. Status column is Empty in each Row. After setting this to Associated.

From the above scenario what I could infer is that when you make changes in the second grid view (which shows the student info) and save it successfully, then value of Label column 'Status' in the parent grid view should be modified to 'True'.

Possible fixes:

  1. Since the column 'Status' is not a bound field and it is being set depending on the condition you should set this value each and every time when the gridview is loaded. So you can do this either in page_load function or in Grid View Load function as the grid view will not know that this value has been modified earlier unless you set this value manually.
  2. Instead you can also change the field 'Status' as bound so that you could retrieve or modify the value in the dataset (as you use it here) and can always have the updated value shown in the gridview 'Status' column without doing it manually.

Note: I could help you with the code for the same if you could post some more code on what is happening.

Solution Part2:

Try the below:

Let us suppose you have put the 'Code to bind first gridview' in the method bindTheGriView() . With this assumption the below code could work fine. Please modify it according to your code.

btnSave_Click method:

protected void btnSave_Click(object sender, EventArgs e)
{
    bool statusFlag=false;
    if (ViewState["RowIndexPOS"] != null)
    {
        int iRowIndex = Convert.ToInt32(ViewState["RowIndexPOS"]);
        Label lblStatus = (Label)GridViewMultiplePOSAssociationId.Rows[iRowIndex].FindControl("LabelStatusPendingPOSId");
        //Means all rows in GridView are successfully associated
        if (table.Rows.Count == iResultCount)
        {
            lblStatus.Text = "Associated";
        }
        else
        {
            lblStatus.Text = "Pending";
        }
    }
    //now call the binding method with the bool flag value
     bindTheGriView();
}

Your method that has the 'Code to bind first gridview' (bindTheGriView(bool statusFlag) in this sample) will look like:

private void bindTheGriView()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("Row Number", typeof(string)));
            dt.Columns.Add(new DataColumn("POS Id", typeof(string)));
            dt.Columns.Add(new DataColumn("Action", typeof(string)));
            dt.Columns.Add(new DataColumn("Status", typeof(string)));
            for (int index = 0; index < m_listStrPendingListOfPOS.Count; index++)
            {
                dr = dt.NewRow();
                int iRowNo = index + 1;
                dr["Row Number"] = iRowNo;
                string strGridViewPOSId = m_listStrPendingListOfPOS[index];
                dr["POS Id"] = strGridViewPOSId;
                dr["Action"] = string.Empty;
                //check for the flag. if the flag is true set status to Pending else to Associated
                dr["Status"]=((Label)GridViewMultiplePOSAssociationId.Rows[index].FindControl("LabelStatusPendingPOSId")).Text;
               dt.Rows.Add(dr);
            }

            ViewState["POSTable"] = dt;
            GridViewMultiplePOSAssociationId.DataSource = dt;
            GridViewMultiplePOSAssociationId.DataBind();
        }

You should call this binding method each and every time you think the Status value has been modified since it is a dynamic data.

Also call the function bindTheGriView() in the Page_Load method

Asssuming that you are using BoundFields for the your Link Button columns and you want to handle the LinkButton-click event (instead of the eg RowCommand of the GridView):

protected void LinkClicked(Object sender, EventArgs e)
{
    //After Populating your Second Gridview
    var closeLink = (Control) sender;
    GridViewRow row = (GridViewRow) closeLink.NamingContainer;
    dataGridView1.Rows[rowNum].Cells[colNum].Text = "True"; 
}

保存第一个gridview的数据时,在这种情况下,更改要绑定第二个gridview的数据集,则必须在将数据保存到第一个gridview时加载第二个gridview,因此没有其他选择。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM