简体   繁体   English

在C#中更改GridView列值

[英]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. 我有一个GridView,它具有一列Status,在每一行中的默认值为False(在标签中)。 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. 将打开一个gridview以显示该用户的数据。 I change data for second GridView and then save it. 我更改第二个GridView的数据,然后将其保存。 So When this data is successfully saved. 因此,当此数据成功保存时。 I want to modify the Status of 1st GridView from False to True for that particular row. 我想将该特定行的第一个GridView的状态从False修改为True。 So please suggest me how to do it? 所以,请建议我该怎么做? Is it necessary to bind the GridView again. 是否有必要再次绑定GridView。 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. 因此,主要问题是如何基于一些操作来修改GridView中的特定单元格值。

I'm writing the piece of Code, which I'm trying. 我正在写一段我正在尝试的代码。 I 'm saving the Parent GridView Row no. 我要保存父级GridView行号。 is ViewState. 是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 绑定第一个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'. 从上面的场景中,我可以推断出,当您在第二个网格视图(显示学生信息)中进行更改并成功保存后,父网格视图中的Label列“ Status”的值应修改为“ 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. 由于“状态”列不是绑定字段,而是根据条件进行设置的,因此每次加载gridview时都应设置此值。 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. 因此,您可以在page_load函数或Grid View Load函数中执行此操作,因为除非手动设置此值,否则网格视图将不知道此值已被更早修改。
  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: 解决方案第2部分:

Try the below: 请尝试以下方法:

Let us suppose you have put the 'Code to bind first gridview' in the method bindTheGriView() . 让我们假设您已将“代码绑定到第一个gridview”放入bindTheGriView()方法中。 With this assumption the below code could work fine. 在这种假设下,以下代码可以正常工作。 Please modify it according to your code. 请根据您的代码对其进行修改。

btnSave_Click method: btnSave_Click方法:

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: 您的方法具有“绑定第一个gridview的代码”(此示例中的bindTheGriView(bool statusFlag))将如下所示:

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 还要在Page_Load方法中调用函数bindTheGriView()

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): 假设您正在对“链接按钮”列使用BoundFields,并且想要处理LinkBut​​ton-click事件(而不是GridView的例如RowCommand):

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,因此没有其他选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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