绑定gridview时会在进程中抛出错误吗?

时间:2014-09-30 07:05:49

标签: javascript c# asp.net asp.net-mvc-4

点击按钮,我将选中的数据从网格复制到另一个网格。我使用postgre SQl作为我的应用程序。以下是HTML的链接。我正在将数据绑定为

 NpgsqlDataAdapter adp;
    NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["projop"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindPrimaryGrid();
            BindSecondaryGrid();
        }
    }

     private void BindSecondaryGrid()
    {
        DataTable dt = (DataTable)ViewState["SelectedRecords"];
        gvSelected.DataSource = dt;
        gvSelected.DataBind();
    }
    private void BindPrimaryGrid()
    {
        DataTable dt = new DataTable();
        adp = new NpgsqlDataAdapter("select user_id, username,screen_name from users", conn);
        dt = new DataTable("users");
        adp.Fill(dt);
        gvAll.DataSource = dt;
        gvAll.DataBind();

选择数据后&按钮单击为

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        GetData();
        SetData();
        BindSecondaryGrid();
    }
private void GetData()
    {
        DataTable dt;
        if (ViewState["SelectedRecords"] != null)
            dt = (DataTable)ViewState["SelectedRecords"];
        else
            dt = CreateDataTable();
        CheckBox chkAll = (CheckBox)gvAll.HeaderRow
                            .Cells[0].FindControl("chkAll");
        for (int i = 0; i < gvAll.Rows.Count; i++)
        {
            if (chkAll.Checked)
            {
                dt = AddRow(gvAll.Rows[i], dt);
            }
            else
            {
                CheckBox chk = (CheckBox)gvAll.Rows[i]
                                .Cells[0].FindControl("chk");
                if (chk.Checked)
                {
                    dt = AddRow(gvAll.Rows[i], dt);
                }
                else
                {
                    dt = RemoveRow(gvAll.Rows[i], dt);
                }
            }
        }
        ViewState["SelectedRecords"] = dt;
    }

    private void SetData()
    {
        CheckBox chkAll = (CheckBox)gvAll.HeaderRow.Cells[0].FindControl("chkAll");
        chkAll.Checked = true;
        if (ViewState["SelectedRecords"] != null)
        {
            DataTable dt = (DataTable)ViewState["SelectedRecords"];
            for (int i = 0; i < gvAll.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvAll.Rows[i].Cells[0].FindControl("chk");
                if (chk != null)
                {
                    DataRow[] dr = dt.Select("CustomerID = '" + gvAll.Rows[i].Cells[1].Text + "'");
                    chk.Checked = dr.Length > 0;
                    if (!chk.Checked)
                    {
                        chkAll.Checked = false;
                    }
                }
            }
        }
    }

此处通过btnSubmit_Click在函数BindSecondaryGrid()中绑定数据,同时在函数gvSelected.DataBind();中显示BindSecondaryGrid()上的错误。

错误:DataBinding: 'HttpException - System.Data.DataRowView' does not contain a property with the name 'user_id'.

编辑:添加&amp;删除代码

  private DataTable AddRow(GridViewRow gvRow, DataTable dt)
    {
        DataRow[] dr = dt.Select("CustomerID = '" + gvRow.Cells[1].Text + "'");
        if (dr.Length <= 0)
        {
            dt.Rows.Add();
            dt.Rows[dt.Rows.Count - 1]["CustomerID"] = gvRow.Cells[1].Text;
            dt.Rows[dt.Rows.Count - 1]["ContactName"] = gvRow.Cells[2].Text;
            dt.Rows[dt.Rows.Count - 1]["Complete Name"] = gvRow.Cells[3].Text;
            dt.AcceptChanges();
        }
        return dt;
    }

    private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
    {
        DataRow[] dr = dt.Select("CustomerID = '" + gvRow.Cells[1].Text + "'");
        if (dr.Length > 0)
        {
            dt.Rows.Remove(dr[0]);
            dt.AcceptChanges();
        }
        return dt;
    }

2 个答案:

答案 0 :(得分:1)

这是因为GridView中指定的DataField不正确。我们来试试吧。

<asp:BoundField DataField = "CustomerID" HeaderText = "Customer ID" 
 HtmlEncode = "false" />
<asp:BoundField DataField = "ContactName" HeaderText = "Contact Name" 
HtmlEncode = "false" />
<asp:BoundField DataField = "CompleteName" HeaderText = "Complete Name" 
HtmlEncode = "false" />

请注意,指定的DataField必须与DataRow的名称匹配。

我假设您的数据表的列以**突出显示:

dt.Rows[dt.Rows.Count - 1]["**CustomerID**"] = gvRow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["**ContactName**"] = gvRow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["**CompleteName**"] = gvRow.Cells[3].Text;

如果这解决了这个问题,请告诉我。感谢

答案 1 :(得分:0)

你能分享你的aspx部分进行网格视图控制吗?

您可以在调试模式下查看数据表数据。检查您是否能够查看用于绑定第二个网格的视图状态的所有列和数据。您应该可以通过执行上述步骤轻松识别问题。