locked
Insert ASP:GridView Into SQL Server Table RRS feed

  • Question

  • User-410261901 posted

    I am attempting to take all checked rows from an asp:gridview and insert them into a datatable then use sqlbulkcopy to insert the data table into sql server.  The issue that I have is that null is always inserted into the database.

    This is how I am attempting to populate the data table from my asp:gridview checked columns.  Does code have issue?  How do I remedy?

    bool columnsAdded = false;
    DataTable dtMain = new DataTable(); foreach (GridViewRow row in gvData.Rows) { CheckBox chk = (CheckBox)row.Cells[0].FindControl("checkboxingrid"); if (chk.Checked == true) { if (!columnsAdded ) { for (int i = 0; i < gvData.Columns.Count; i++) { dtMain.Columns.Add("column" + i.ToString()); } columnsAdded = true; } DataRow dr = dtMain.NewRow(); for (int j = 0; j < gvData.Columns.Count; j++) { dr["column" + j.ToString()] = row.Cells[j].Text; } dtMain.Rows.Add(dr); } }

    Friday, January 12, 2018 12:27 AM

Answers

  • User-1716253493 posted
            DataTable dtMain = new DataTable();
            for (int i = 1; i < gvData.Columns.Count; i++) //assume first column is checkbox (not included)
            {
                dtMain.Columns.Add("column" + i.ToString());
            }
            foreach (GridViewRow row in gvData.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("checkboxingrid"); //find it from row
                if (chk.Checked)
                {
                    DataRow dr = dtMain.NewRow();
                    for (int i = 1; i < gvData.Columns.Count; i++)
                    {
                        dr[i-1] = row.Cells[i].Text; //Column1 value from columns[1]
                    }
                    dtMain.Rows.Add(dr);
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 12, 2018 1:44 AM
  • User-1716253493 posted

    You can check

    if(gvData.Columns[i].Visible)
    {
        //Add datacolomn
    }

    But you must remember to check visibility alsi when add datarow

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 12, 2018 9:26 AM

All replies

  • User-1716253493 posted

    Seem like you want like this

            DataTable dtMain = new DataTable();
            for (int i = 0; i < gvData.Columns.Count - 1; i++) //assume last column is checkbox
            {
                dtMain.Columns.Add("column" + i.ToString());
            }
            foreach (GridViewRow row in gvData.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("checkboxingrid"); //find it from row
                if (chk.Checked)
                {
                    DataRow dr = dtMain.NewRow();
                    for (int i = 0; i < gvData.Columns.Count - 1; i++)
                    {
                        dr[i] = row.Cells[i].Text;
                    }
                    dtMain.Rows.Add(dr);
                }
            }

    Friday, January 12, 2018 1:34 AM
  • User-410261901 posted

    @oned_gk - the first column is the checkbox.  How would that deviate from the syntax you posted above?

    Friday, January 12, 2018 1:37 AM
  • User-1716253493 posted
            DataTable dtMain = new DataTable();
            for (int i = 1; i < gvData.Columns.Count; i++) //assume first column is checkbox (not included)
            {
                dtMain.Columns.Add("column" + i.ToString());
            }
            foreach (GridViewRow row in gvData.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("checkboxingrid"); //find it from row
                if (chk.Checked)
                {
                    DataRow dr = dtMain.NewRow();
                    for (int i = 1; i < gvData.Columns.Count; i++)
                    {
                        dr[i-1] = row.Cells[i].Text; //Column1 value from columns[1]
                    }
                    dtMain.Rows.Add(dr);
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 12, 2018 1:44 AM
  • User-410261901 posted

            DataTable dtMain = new DataTable();
            for (int i = 1; i < gvData.Columns.Count; i++) //assume first column is checkbox (not included)
            {
                dtMain.Columns.Add("column" + i.ToString());
            }
            foreach (GridViewRow row in gvData.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("checkboxingrid"); //find it from row
                if (chk.Checked)
                {
                    DataRow dr = dtMain.NewRow();
                    for (int i = 1; i < gvData.Columns.Count; i++)
                    {
                        dr[i-1] = row.Cells[i].Text; //Column1 value from columns[1]
                    }
                    dtMain.Rows.Add(dr);
                }
            }

    Is there a way to only add the visible columns of the gridview to the datatable?  This above s yntax adds allc olumns even the hiddens.

    Friday, January 12, 2018 2:40 AM
  • User-1716253493 posted

    You can check

    if(gvData.Columns[i].Visible)
    {
        //Add datacolomn
    }

    But you must remember to check visibility alsi when add datarow

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 12, 2018 9:26 AM