2
Answers

Send the checked Rows of dataGridview columns data To DB

Photo of ABHI  HBK

ABHI HBK

12y
1.6k
1
//on button click
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.Cells[0].FindControl("chkSelect");
if (chk != null && chk.Checked)
{
//insert into table1 values('" + row.Cells[0].text + "' ,'" + row.Cells[1].text + "' ) 
}

 
Here Is The code written for GridView with CheckBox in C# web application_

But i want to perform this Task In Windows Application in c#_
 
Help Me_ Smile | :) 
TO Complete This Task_

Thank you_

Answers (2)

1
Photo of Satyapriya Nayak
NA 53k 8m 12y
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Checked_record__datagridview
{
    public partial class Form1 : Form
    {
        OleDbDataAdapter oledbda;
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        string str;
        OleDbCommand com;

        public Form1()
        {
            InitializeComponent();
        }

        private void btn_insert_Click(object sender, EventArgs e)
        {
            int i = 0;
            List<int> ChkedRow = new List<int>();

            for (i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true)
                {
                    ChkedRow.Add(i);
                }
            }

            if (ChkedRow.Count == 0)
            {
                MessageBox.Show("Select one checkbox");
                return;
            }

            foreach (int j in ChkedRow)
            {
              
                str = @"INSERT INTO test1(Did,Dname) VALUES ('" + dataGridView1.Rows[j].Cells["Did"].Value + "', '" + dataGridView1.Rows[j].Cells["Dname"].Value + "');";
                try
                {
                    using (OleDbConnection con = new OleDbConnection(ConnectionString))
                    {
                        using (com = new OleDbCommand(str, con))
                        {
                            con.Open();
                            com.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            MessageBox.Show("Records successfully inserted");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Columns.Clear();
            DataGridViewCheckBoxColumn col1 = new DataGridViewCheckBoxColumn();
            col1.Name = "chkcol";
            col1.HeaderText = "CheckBox";
            dataGridView1.Columns.Add(col1);
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from test ";
            com = new OleDbCommand(str, con);
            oledbda = new OleDbDataAdapter(com);
            DataSet ds = new DataSet();
            oledbda.Fill(ds, "test");
            dataGridView1.DataMember = "test";
            dataGridView1.DataSource = ds;

        }
    }
}

Accepted
0
Photo of ABHI  HBK
NA 3 1.6k 12y
It's totally right_ Thank U_ But in your code you have used OleDb connection, actually i had used Sql connection and Database of type mdf. Not a big problem i had replaced OleDb with Sql and its working too, but the selected CheckBox values are saving as 'True' in the DataBase, not the actual column value which i had selected...  

for example i had selected in the course column BCA and marked it and sved it, but in the DataBase it is not saving as BCA it's taking the value as 'True'.

Do You have some solutions for this_