Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 632517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:05:07+00:00 2026-05-13T20:05:07+00:00

I have a gridview which will contain some ‘n’ number of rows…. Now i

  • 0

I have a gridview which will contain some ‘n’ number of rows…. Now i want to add all rows of the gridview to a datatable which will be used for bulkcopy operation…

I have found this http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx

But i want all columns of my gridview to be added to the datarow of the datatable
Grid http://img85.imageshack.us/img85/4044/gridp.jpg

I want to convert gridview to datatable on submit…. Any suggestion…

EDIT:

Answer below works and i have found an answer too…

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
    dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("DaysPresent", typeof(double)));
    dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
    dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
    dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
    dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
    dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));

    foreach (GridViewRow row in gridEmployee.Rows) 
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            DataRow dr = dt.NewRow();
            dr["EmpId"] = Convert.ToInt64(((HiddenField)row.Cells[0].FindControl("HiddenId")).Value);
            dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
            dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
            dr["DaysPresent"] = Convert.ToDouble(((TextBox)row.Cells[3].FindControl("TxtDaysPresent")).Text);
            dr["OpeningAdvance"] = Convert.ToDouble(((TextBox)row.Cells[4].FindControl("txtOpeningAdv")).Text);
            dr["AdvanceDeducted"] = Convert.ToDouble(((TextBox)row.Cells[5].FindControl("TxtAdvanceDeducted")).Text);
            dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);
            dr["SalaryGiven"] = Convert.ToDouble(((TextBox)row.Cells[7].FindControl("TxtSalary")).Text);
            dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
            dt.Rows.Add(dr);
        }
    }
    SqlBulkCopy sbc = new SqlBulkCopy(connectionString);
    sbc.DestinationTableName = "SalaryDetails";
    sbc.ColumnMappings.Add("EmpId", "EmpId");
    sbc.ColumnMappings.Add("FromDate", "FromDate");
    sbc.ColumnMappings.Add("ToDate", "ToDate");
    sbc.ColumnMappings.Add("DaysPresent", "DaysPresent");
    sbc.ColumnMappings.Add("OpeningAdvance", "OpeningAdvance");
    sbc.ColumnMappings.Add("AdvanceDeducted", "AdvanceDeducted");
    sbc.ColumnMappings.Add("RemainingAdvance", "RemainingAdvance");
    sbc.ColumnMappings.Add("SalaryGiven", "SalaryGiven");
    sbc.ColumnMappings.Add("CreatedDate", "CreatedDate");
    sbc.WriteToServer(dt);
    sbc.Close();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:05:08+00:00Added an answer on May 13, 2026 at 8:05 pm

    you can traverse datagrid row by row and make a comma separated file. then use Bulk insert or bcp for inserting data to db.

    Another Solution

        DataTable dt = new DataTable();    
        for (int j = 0; j < grdList.Rows.Count; j++)
        {
            DataRow dr;
            GridViewRow row = grdList.Rows[j];
            dr = dt.NewRow();
            for (int i = 0; i < row.Cells.Count; i++)
            {
                dr[i] = row.Cells[i].Text;
            }
    
            dt.Rows.Add(dr);
        }
    
    SqlBulkCopy sbc = new SqlBulkCopy(targetConnStr);
    sbc.DestinationTableName = "yourDestinationTable";
    sbc.WriteToServer(dt);
    sbc.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a gridview control which will show X amount of rows. I want to
I have a gridview which displays rows and columns all linked to an sql
I have a GridView which may contain 10's and 100's of rows, on each
I have a GridView which is showing some data: Entity_ID (PK) Name Description Now
i have a GridView (selectable) in which I want to generate a dynamic GridView
I have a GridView which retrieves data from a database. Users have roles, some
I have a gridview which I want to zebra stripe after it has been
I have a gridview which will have Insert/Delete/Update. There are two kinds of exception
I have a textbox which will contain phone numbers. I need to fill a
I have a GridView which is programatically filled from the DB (not a SqlDataSource

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.