Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
C#
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
C#
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;
    }
Posted
Comments
ChintanShukla 30-Sep-14 5:39am    
Can you Provide designer Page Code?

1 solution

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'
 
Share this answer
 
Comments
suneet.saini 30-Sep-14 7:14am    
both the Grid having this 'user_id' column. I had also given the link of the HTML which I am using in this code.
ChintanShukla 30-Sep-14 7:27am    
Not Grid. DataTable dt in your case. When you cast it from ViewSource does it have column names 'user_id'
suneet.saini 30-Sep-14 8:02am    
In the HTML linked to this question shows that it contains 'user_id' column.
ChintanShukla 30-Sep-14 8:14am    
I am talking about datatable dt. Apply a break point and open visualizer and check the column name

To use it, break into your code, mouse over your DataTable, expand the quick watch, view the Tables, expand that, then view dt . You will see something like {Table1} in the quick watch, but notice that there is also a magnifying glass icon. Click on that icon and your DataTable will open up in a grid view and then check column name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900