Hi shabzo,
Refer the below sample code.
Form1.cs
public partial class Form1 : Form
{
static DataTable dt = new DataTable();
private const string ConnectionString = @"Data Source=.;DataBase=Northwind;UID=sa;PWD=123";
public Form1()
{
InitializeComponent();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Address"), new DataColumn("City"), new DataColumn("Country") });
}
private void btnFirst_Click(object sender, EventArgs e)
{
BindDataGrid("SELECT TOP 10 CustomerID Id,Address,City,Country FROM Customers");
}
private void btnSecond_Click(object sender, EventArgs e)
{
BindDataGrid("SELECT TOP 10 StudentID Id,Address,City,Country FROM Students");
}
private void btnThird_Click(object sender, EventArgs e)
{
BindDataGrid("SELECT TOP 9 EmployeeID Id,Address,City,Country FROM Employees");
}
private void BindDataGrid(string query)
{
dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();
dataGridView1.ColumnCount = 4;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns[0].Name = "Id";
dataGridView1.Columns[0].HeaderText = "Id";
dataGridView1.Columns[0].DataPropertyName = "Id";
dataGridView1.Columns[0].Width = 50;
dataGridView1.Columns[1].HeaderText = "Address";
dataGridView1.Columns[1].Name = "Address";
dataGridView1.Columns[1].DataPropertyName = "Address";
dataGridView1.Columns[1].Width = 100;
dataGridView1.Columns[2].Name = "City";
dataGridView1.Columns[2].HeaderText = "City";
dataGridView1.Columns[2].DataPropertyName = "City";
dataGridView1.Columns[2].Width = 100;
dataGridView1.Columns[3].Name = "Country";
dataGridView1.Columns[3].HeaderText = "Country";
dataGridView1.Columns[3].DataPropertyName = "Country";
dataGridView1.Columns[3].Width = 75;
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "Select";
checkBoxColumn.Width = 50;
checkBoxColumn.Name = "checkBoxColumn";
dataGridView1.Columns.Insert(0, checkBoxColumn);
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells["checkBoxColumn"].EditedFormattedValue.ToString().ToLower() == "true")
{
dt.Rows.Add(dataGridView1.Rows[e.RowIndex].Cells[1].Value, dataGridView1.Rows[e.RowIndex].Cells[2].Value, dataGridView1.Rows[e.RowIndex].Cells[3].Value, dataGridView1.Rows[e.RowIndex].Cells[4].Value);
}
else
{
DataRow[] dataRows = dt.Select(string.Format("Id='{0}' and Address='{1}' and City='{2}' and Country='{3}'", dataGridView1.Rows[e.RowIndex].Cells[1].Value, dataGridView1.Rows[e.RowIndex].Cells[2].Value, dataGridView1.Rows[e.RowIndex].Cells[3].Value, dataGridView1.Rows[e.RowIndex].Cells[4].Value));
foreach (DataRow dataRow in dataRows)
{
dt.Rows.Remove(dataRow);
}
}
}
private void btnSend_Click(object sender, EventArgs e)
{
DataView dw = new DataView(dt);
string[] cols = { "Id", "Address", "City", "Country" };
DataTable dt1 = dw.ToTable(true, cols);
if (dt1.Rows.Count > 0)
{
Form2 f = new Form2(dt1);
f.ShowDialog();
}
else
{
MessageBox.Show("Select atleast one record");
}
}
}
Form2.cs
public partial class Form2 : Form
{
public Form2(DataTable dt)
{
InitializeComponent();
dataGridView1.DataSource = dt;
}
}
Screenshot