Pass Checked DataGridView row values from one form to another using C# and VB.Net in Windows Application

pratikshir
 
on Aug 23, 2021 08:15 AM
Sample_956309.zip
541 Views

how to pass value from CheckBox gridview Form1 to another form

PopUpForm.cs | 

        private void btn_SelectGet_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                bool IsSelected = Convert.ToBoolean(row.Cells["Col_CB"].Value);
                if (IsSelected)
                {
                    SetValueForlblPrimaryKey = row.Cells[1].Value.ToString();
                    StdFrm.Activate();
                    StdFrm.SearchByStudID();
                }             
            }            
        }

Form2 (AddStudent.cs )

        public void SearchByStudID()
        {
            da = new SqlDataAdapter("Select * FROM Tbl_Student Where StudID=N'" + lblPrimaryKey.Text + "'", con.Get_ConnectionUWS());
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                lblPrimaryKey.Text = dt.Rows[0]["StudID"].ToString();
                lbl_StudIDNo.Text = dt.Rows[0]["StudNo"].ToString();
                TxtStudIDNo.Text = dt.Rows[0]["StudUniqueNo"].ToString();
                TxtStudAdharNo.Text = dt.Rows[0]["StudAdharNo"].ToString();
                string Name = Convert.ToString(dt.Rows[0]["StudName"]);
                string[] NameSplit = Name.Split(' ');
                TxtSurname.Text = NameSplit[0];
                TxtFirstName.Text = NameSplit[1];
                TxtMiddleName.Text = NameSplit[2];

                CmbInitial.Text = dt.Rows[0]["StuInti"].ToString();

                if (Convert.ToString(dt.Rows[0]["Gender"]) == "F")
                    rdb_Female.Checked = true;
                else if (Convert.ToString(dt.Rows[0]["Gender"]) == "M")
                    rdb_Male.Checked = true;

                TxtMotherName.Text = Convert.ToString(dt.Rows[0]["StudMotherName"]);
                TxtNationality.Text = Convert.ToString(dt.Rows[0]["StudNationality"]);
                if (dt.Rows[0]["MotherTounghID"] != DBNull.Value)
                    cmbMotherToungh.SelectedValue = Convert.ToInt32(dt.Rows[0]["MotherTounghID"]);
                if (dt.Rows[0]["ReligionID"] != DBNull.Value)
                    cmbReligion.SelectedValue = Convert.ToInt32(dt.Rows[0]["ReligionID"]);
                if (dt.Rows[0]["CastID"] != DBNull.Value)
                    cmbCast.SelectedValue = Convert.ToInt32(dt.Rows[0]["CastID"]);
                cmbSubCast.Text = Convert.ToString(dt.Rows[0]["SubCast"]);
                //if (dt.Rows[0]["TrustId"] != DBNull.Value)
                //    cmbTrust.SelectedValue = Convert.ToInt32(dt.Rows[0]["TrustId"]);
                TxtBirthPlace.Text = Convert.ToString(dt.Rows[0]["StudBirthPlace"]);
                if (dt.Rows[0]["TalukaID"] != DBNull.Value)
                    cmbTaluka.SelectedValue = Convert.ToInt32(dt.Rows[0]["TalukaID"]);
                if (dt.Rows[0]["CityID"] != DBNull.Value)
                    cmbDist.SelectedValue = Convert.ToInt32(dt.Rows[0]["CityID"]);
                if (dt.Rows[0]["StateID"] != DBNull.Value)
                    CmbState.SelectedValue = Convert.ToInt32(dt.Rows[0]["StateID"]);

                if (dt.Rows[0]["CountryID"] != DBNull.Value)
                    cmbCountry.SelectedValue = Convert.ToInt32(dt.Rows[0]["CountryID"]);
                dtpdob.Text = Convert.ToString(dt.Rows[0]["StudDOB"]);
                TxtDOBInWords.Text = Convert.ToString(dt.Rows[0]["DOBInWords"]);
                TxtPreviousSchool.Text = Convert.ToString(dt.Rows[0]["PreviousSchool"]);
                if (dt.Rows[0]["PreviousSchoolClass"] != DBNull.Value)
                    cmbPreviousStd.SelectedValue = Convert.ToInt32(dt.Rows[0]["PreviousSchoolClass"]);
                dtpAdmissiondt.Text = Convert.ToString(dt.Rows[0]["AdmissionDate"]);
                if (dt.Rows[0]["AdmissionClass"] != DBNull.Value)
                    cmbAdmissionStd.Text = Convert.ToString(dt.Rows[0]["AdmissionClass"]);
                TxtStudyDevelopment.Text = Convert.ToString(dt.Rows[0]["StudyDevelopment"]);
                if (dt.Rows[0]["StudBehaviour"] != DBNull.Value)
                    cmbBehaviour.Text = Convert.ToString(dt.Rows[0]["StudBehaviour"]);
                dtpSchoolLeavingDt.Text = Convert.ToString(dt.Rows[0]["LeavingDate"]);

                if (dt.Rows[0]["WasStudying"] != DBNull.Value)
                    cmbLeavingStd.Text = Convert.ToString(dt.Rows[0]["WasStudying"]);
                ChkLeavingCmb.Checked = true;

                TxtSince.Text = Convert.ToString(dt.Rows[0]["WasStudyingSince"]);

                TxtSchoolLeavingReason.Text = Convert.ToString(dt.Rows[0]["LeavingReason"]);
                TxtRemark.Text = Convert.ToString(dt.Rows[0]["SchoolRemark"]);

                if (dt.Rows[0]["SchoolID"] != DBNull.Value)
                    cmbSchoolCollege.SelectedValue = Convert.ToString(dt.Rows[0]["SchoolID"]);
                TxtBirthSign1.Text = Convert.ToString(dt.Rows[0]["BirthSign1"]);
                TxtBirthSign2.Text = Convert.ToString(dt.Rows[0]["BirthSign2"]);
                TxtMobile.Text = Convert.ToString(dt.Rows[0]["MobileContact"]);
                TxteMail.Text = Convert.ToString(dt.Rows[0]["Email"]);
                if (dt.Rows[0]["CategoryID"] != DBNull.Value)
                    cmbCategory.SelectedValue = Convert.ToInt32(dt.Rows[0]["CategoryID"]);
                if (dt.Rows[0]["AcadamicYrID"] != DBNull.Value)
                    cmbCurrentEduYr.SelectedValue = Convert.ToInt32(dt.Rows[0]["AcadamicYrID"]);
                cmbCurrentStd.Text = Convert.ToString(dt.Rows[0]["CurrentStd"]);
                TxtOnlineStudNo.Text = Convert.ToString(dt.Rows[0]["OnlineStudNo"]);
                TxtGenralRegNo.Text = Convert.ToString(dt.Rows[0]["GeneralRegNo"]);
                lbl_TCCount.Text = Convert.ToString(dt.Rows[0]["TCCount"]);
            }
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 24, 2021 05:04 AM

Hi pratikshir,

Check this example. Now please take its reference and correct your code.

Code

Form1

C#

public partial class Form1 : Form
{
    public static string id { get; set; }
    public Form1()
    {
        InitializeComponent();
        this.CenterToScreen();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.BindDataGridView();
    }

    private void BindDataGridView()
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
            new DataColumn("Name", typeof(string)),
            new DataColumn("Country",typeof(string)) });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        this.dataGridView1.DataSource = dt;
        DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
        checkBoxColumn.HeaderText = "";
        checkBoxColumn.Width = 30;
        checkBoxColumn.Name = "checkBoxColumn";
        dataGridView1.Columns.Insert(0, checkBoxColumn);
    }

    private void btnTransfer_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
            if (isSelected)
            {
                id = row.Cells[1].Value.ToString();
                Form2 form2 = new Form2();
                form2.Show();
                break;
            }
        }
    }
}

VB.Net

Imports System.Data

Public Class Form1
    Public Shared Property id As String

    Public Sub New()
        InitializeComponent()
        Me.CenterToScreen()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.BindDataGridView()
    End Sub

    Private Sub BindDataGridView()
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        Me.dataGridView1.DataSource = dt
        Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
        checkBoxColumn.HeaderText = ""
        checkBoxColumn.Width = 30
        checkBoxColumn.Name = "checkBoxColumn"
        dataGridView1.Columns.Insert(0, checkBoxColumn)
    End Sub

    Private Sub btnTransfer_Click(ByVal sender As Object, ByVal e As EventArgs)
        For Each row As DataGridViewRow In dataGridView1.Rows
            Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)

            If isSelected Then
                id = row.Cells(1).Value.ToString()
                Dim form2 As Form2 = New Form2()
                form2.Show()
                Exit For
            End If
        Next
    End Sub
End Class

Form2

C#

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        this.CenterToScreen();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
            new DataColumn("Name", typeof(string)),
            new DataColumn("Country",typeof(string)) });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        DataRow[] dr = dt.Select("Id='" + Form1.id + "'");
        if (dr.Length > 0)
        {
            lblId.Text = dr[0]["Id"].ToString();
            txtName.Text = dr[0]["Name"].ToString();
            txtCountry.Text = dr[0]["Country"].ToString();
        }
    }
}

VB.Net

Public Class Form2
    Public Sub New()
        InitializeComponent()
        Me.CenterToScreen()
    End Sub

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        Dim dr As DataRow() = dt.[Select]("Id='" & Form1.id & "'")

        If dr.Length > 0 Then
            lblId.Text = dr(0)("Id").ToString()
            txtName.Text = dr(0)("Name").ToString()
            txtCountry.Text = dr(0)("Country").ToString()
        End If
    End Sub
End Class

Screenshot