网格视图数据和选定的索引不同步

时间:2014-10-28 18:43:32

标签: c# asp.net

我目前有一个C#ASP.NET应用程序,主屏幕是网格视图,如下所示:

<asp:GridView ID="tblTasks" Height="50px" Width="100%" AutoGenerateEditButton="true" AutoGenerateColumns="false" gridlines="None" emptyDataText="No Records Found" ShowHeaderWhenEmpty="True" runat="server" OnRowCreated="tblTasks_RowCreated" OnRowEditing="tblTasks_RowEditing">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-CssClass="datacol" HeaderStyle-CssClass="headingcol" />
            <asp:BoundField DataField="Customer Name" HeaderText="Customer Name" ItemStyle-CssClass="datacol" HeaderStyle-CssClass="headingcol" />
            <asp:BoundField DataField="Who Took Call" HeaderText="Who Took Call" ItemStyle-CssClass="datacol" HeaderStyle-CssClass="headingcol" />
            <asp:BoundField DataField="Date Entered" HeaderText="Date Entered" ItemStyle-CssClass="datacol" HeaderStyle-CssClass="headingcol" />
            <asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-CssClass="datacol" HeaderStyle-CssClass="headingcol" />
            <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-CssClass="datacol" HeaderStyle-CssClass="headingcol" />
            <asp:BoundField DataField="Date Entered" HeaderText="Date Entered"  ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
            <asp:BoundField DataField="Dispatched To" HeaderText="Dispatched To"  ItemStyle-CssClass="datacol" HeaderStyle-CssClass="headingcol" />
            <asp:BoundField DataField="Resolution" HeaderText="Resolution" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
            <asp:BoundField DataField="Date Completed" HeaderText="Date Completed" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
        </Columns>
    </asp:GridView>

然后我在页面加载时通过存储过程填充网格视图:

myGridView grid = new myGridView(tblTasks, "p_select_all_tasks");

传递给这个类/方法:

public myGridView(GridView argGrid, string argStoredProcedure)
    {
        this.dataGrid = argGrid;
        this.storedProcedure = argStoredProcedure;
        this.connection = GeneralUtility.getConnectionString();
        fill();
    }

    public myGridView(GridView argGrid, string argStoredProcedure, List<SqlParameter> argParamter)
    {
        this.dataGrid = argGrid;
        this.storedProcedure = argStoredProcedure;
        this.connection = GeneralUtility.getConnectionString();
        this.parameter = argParamter;
        fill();
    }

    private void fill()
    {
        using (myDatabase db = new myDatabase(connection))
        {
            this.dataGrid.DataSource = null;
            this.dataGrid.DataSource = db.ExecuteCommandWithDSReturn(storedProcedure, parameter);
            this.dataGrid.DataBind();
        }
    }

现在在主页面的顶部有一些用户可以从中选择值的下拉菜单,然后单击更新按钮以使用此代码更改显示的网格视图中的值:

protected void btn_Click_Return_Tasks(object sender, EventArgs e)
    {
        String userToFetchTasksFor = userSelectionDropDown.SelectedValue;
        String statusToFetchTasksFor = statusSelectionDropDown.SelectedValue;

        if (!userToFetchTasksFor.Equals("All") && !statusToFetchTasksFor.Equals("All"))
        {
            List<SqlParameter> tmpParams = new List<SqlParameter>();
            tmpParams.Add(new SqlParameter("@user_name", userToFetchTasksFor));
            tmpParams.Add(new SqlParameter("@status", statusToFetchTasksFor));
            myGridView grid = new myGridView(tblTasks, "p_select_task_by_user_name_and_status", tmpParams);             
        }
        else if (userToFetchTasksFor.Equals("All") && !statusToFetchTasksFor.Equals("All"))
        {
            List<SqlParameter> tmpParams = new List<SqlParameter>();
            tmpParams.Add(new SqlParameter("@status", statusToFetchTasksFor));
            myGridView grid = new myGridView(tblTasks, "p_select_task_by_status", tmpParams);
        }
        else if (!userToFetchTasksFor.Equals("All") && statusToFetchTasksFor.Equals("All"))
        {
            List<SqlParameter> tmpParams = new List<SqlParameter>();
            tmpParams.Add(new SqlParameter("@user_name", userToFetchTasksFor));
            myGridView grid = new myGridView(tblTasks, "p_select_task_by_user_name", tmpParams);
        }
        else
        {
            myGridView grid = new myGridView(tblTasks, "p_select_all_tasks");
        }
    }

网格视图重新填充并显示正确的信息。现在来问题了。当我第一次加载页面加载的完整网格视图时,我可以单击编辑按钮并运行以下代码:

protected void tblTasks_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gv = (GridView)sender;

        DataSet ds = (DataSet)gv.DataSource;

        DataTable dt = ds.Tables[0];

        string s = string.Empty;

        NameValueCollection nvc = new NameValueCollection();
        foreach (DataColumn dc in dt.Columns)
        {
            String column = dc.ToString();
            if (dc.ToString().Equals("ID"))
            {
                nvc.Add(dc.ColumnName, tblTasks.Rows[e.NewEditIndex].Cells[1].Text.ToString());
                //String p = tblTasks.Rows[e.NewEditIndex].Cells[1].Text;
            }
        }
        var array = (from key in nvc.AllKeys
                     from value in nvc.GetValues(key)
                     select string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value))).ToArray();

        Response.Redirect(@"/EditTask.aspx?" + string.Join("&", array));
    }

首次加载时效果很好但在通过btn_Click_Return_Tasks命令刷新网格视图后,索引与网格视图中的数据不同步。选定的索引是正确的,但是tblTask​​s.Rows [e.NewEditIndex] .Cells [1] .Text.ToString()值取决于页面首次加载时的表格,而不是更新后的实际行。我该如何解决这个问题?

0 个答案:

没有答案