Click here to Skip to main content
15,878,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi , I create DataGrid view at c # project .and I call data from database tables to the DataGrid view . and added two column check box (AUT,Active) to the DataGrid view , I want to save the data from grid view to table SQL .

pic1 — ImgBB[^]

pic2 — ImgBB[^]

What I have tried:

//1- search code to call the data to the datagridview :-
public DataTable SEARSH_listdgv(string USID)
            {
                var DAL = new SQlcls.DataAccessLayer();
                var DT = new DataTable();
                DT = DAL.SelectData_txt("SELECT  dbo.Company.Id, dbo.Company.NameA, dbo.Branch.ID AS BRID, dbo.Branch.NameA AS BRNAME, dbo.FinancialYear.Id AS YEID, dbo.FinancialYear.NameA AS YENAME, dbo.Users.ID AS USID,  dbo.Users.NameA AS USNAME FROM  dbo.Company INNER JOIN  dbo.Branch ON dbo.Company.CBF = dbo.Branch.CBF INNER JOIN   dbo.FinancialYear ON dbo.Company.CBF = dbo.FinancialYear.CBF INNER JOIN  dbo.Users ON dbo.Company.CBF = dbo.Users.CBF WHERE dbo.Users.ID='" + USID + "'   ", default);
                return DT;

            }
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 //2- i coded public void to fill the datagrid view at loading the form:-

 <pre lang="C#">public void SEARRCH_cbf()
        {
            LOGIN cmp = new LOGIN();
            var DT = new DataTable();
            DT = cmp.SEARSH_listdgv(TXT_ID.Text);
            if (DT.Rows.Count > 0)
            {
                dataGridView1.DataSource = DT;
                var withBlock = dataGridView1;
                withBlock.Columns[3].HeaderCell.Value = "كود الشركة";
                withBlock.Columns[3].DisplayIndex = 0;
                withBlock.Columns[4].HeaderCell.Value = "الشركة";
                withBlock.Columns[4].DisplayIndex = 1;
                withBlock.Columns[5].HeaderCell.Value = "  كود الفرع";
                withBlock.Columns[5].DisplayIndex = 2;
                withBlock.Columns[6].HeaderCell.Value = "الفرع";
                withBlock.Columns[6].DisplayIndex =3;
                withBlock.Columns[7].HeaderCell.Value = "كود السنة";
                withBlock.Columns[7].DisplayIndex = 4;
                withBlock.Columns[8].HeaderCell.Value = "السنة";
                withBlock.Columns[8].DisplayIndex = 5;
                withBlock.Columns[9].HeaderCell.Value = "كود المستخدم";
                withBlock.Columns[9].DisplayIndex = 6;
                withBlock.Columns[10].HeaderCell.Value = "اسم المستخدم";
                withBlock.Columns[10].DisplayIndex = 7;
                withBlock.Columns[0].HeaderCell.Value = "صلاحية ";
                withBlock.Columns[0].DisplayIndex = 8;
                withBlock.Columns[1].HeaderCell.Value = "تنشيط";
                withBlock.Columns[1].DisplayIndex =9;
                withBlock.Columns[2].DisplayIndex = 10;

            }
        }
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//3- load the public void at from load event :-
 private void USERS_Load(object sender, EventArgs e)
        {

            SEARRCH_cbf();
           
            {
                var withBlock = dataGridView1;

            }
            
        }
//3- code for save gridview data to sql table:-
private void button2_Click(object sender, EventArgs e)
        {

            foreach (DataGridViewRow row in dataGridView1.Rows)

            {
               
                string constring = @"Data Source = MOHAMEDTHRWAT20\SQLEXPRESS; Initial Catalog = SharpControl; Integrated Security = True";
                using (SqlConnection con = new SqlConnection(constring))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO CBF VALUES(@UseID, @UseName, @CompID,@Compname,@BranId,@BranName,@YEID,@YEName,@AUT,@Active)", con))
                    {       
cmd.Parameters.AddWithValue("@UseID", row.Cells["USID"].Value);
cmd.Parameters.AddWithValue("@UseName", row.Cells["USNAME"].Value);
cmd.Parameters.AddWithValue("@CompID", row.Cells["Id"].Value);
cmd.Parameters.AddWithValue("@Compname", row.Cells["NameA"].Value);
cmd.Parameters.AddWithValue("@BranId", row.Cells["BRID"].Value);
cmd.Parameters.AddWithValue("@BranName", row.Cells["BRNAME"].Value);
cmd.Parameters.AddWithValue("@YEID", row.Cells["YEID"].Value);
cmd.Parameters.AddWithValue("@YEName", row.Cells["YENAME"].Value);
cmd.Parameters.AddWithValue("@AUT", row.Cells["AUT"].Value);
cmd.Parameters.AddWithValue("@Active", row.Cells["Active"].Value);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();

                    }


                }
            }
        }
Posted
Updated 12-Sep-22 12:12pm
v2
Comments
[no name] 12-Sep-22 23:26pm    
https://www.codeproject.com/Questions/105828/How-can-i-add-a-column-to-an-existing-datatable

Then bind it and the new columns to the data grid.

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