绑定gridview时会在进程中抛出错误吗? [英] While binding the gridview throwing an error in a process?

查看:96
本文介绍了绑定gridview时会在进程中抛出错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

私有 void BindSecondaryGrid( )
{
DataTable dt =(DataTable)ViewState [ SelectedRecords] ;
gvSelected.DataSource = dt;
gvSelected.DataBind();
}
私有 void BindPrimaryGrid()
{
DataTable dt = new DataTable();
adp = new NpgsqlDataAdapter( select user_id ,username,screen_name来自用户,conn);
dt = new DataTable( users );
adp.Fill(dt);
gvAll.DataSource = dt;
gvAll.DataBind();



选择数据后&按钮单击为

  protected   void  btnSubmit_Click ( object  sender,EventArgs e)
{
GetData();
SetData();
BindSecondaryGrid();
}
私人 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]
。单元格[ 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]。单元格[ 0 ]。FindControl( chk);
if (chk!= null
{
DataRow [ ] dr = dt.Select( CustomerID =' + gvAll.Rows [i] .Cells [ 1 ]。文字+ ' );
chk.Checked = dr.Length > 0 ;
if (!chk.Checked)
{
chkAll.Checked = false < /跨度>;
}
}
}
}
}



这里绑定函数中的数据 btnSubmit_Click 通过 BindSecondaryGrid()在函数 BindSecondaryGrid()中的 gvSelected.DataBind(); 上显示错误。



错误:DataBinding:'HttpException - System.Data.DataRowView'不包含名为'user_id'的属性



编辑:添加&删除代码

  private  DataTable AddRow(GridViewRow gvRow,DataTable dt)
{
DataRow [] dr = dt.Select( CustomerID =' + gvRow.Cells [ 1 ]。文本+ ');
if (dr.Length < = 0
{
dt.Rows.Add();
dt.Rows [dt.Rows.Count - 1 ] [ CustomerID] = gvRow.Cells [ 1 ]。文字;
dt.Rows [dt.Rows.Count - 1 ] [ ContactName] = gvRow.Cells [ 2 ]。文字;
dt.Rows [dt.Rows.Count - 1 ] [ 完整名称] = gvRow.Cells [ 3 ]。文字;
dt.AcceptChanges();
}
return dt;
}

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

解决方案

这意味着DataTable中不存在'user_id'column。

您是否确保与之关联的dataTable具有user_id列?



调试并检查您的DataTable是否具有名为user_id的列

I am copying the selcted data from on grid to another on the click of a button. I am using postgre SQl for my apllication. Here is the link for the HTML. I am binding the data in as

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();


After the selection of the data & button click as

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;
                    }
                }
            }
        }
    }


Here while binding the data in function btnSubmit_Click via BindSecondaryGrid() showing an error on gvSelected.DataBind(); in function BindSecondaryGrid().

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

Edit: Add & Remove Codes

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;
    }

解决方案

This means that 'user_id'column is not present in the DataTable.
Have you made sure that your dataTable with which you are associating has the column 'user_id'?

Debug and check whether your DataTable has column named 'user_id'


这篇关于绑定gridview时会在进程中抛出错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆