Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to bind selected row in a listview to a new form(or existing form) in c# windows forms

$
0
0

Hi Everyone

My name is vishal. For the past one week i have been trying to figure out how to bind selected row of a list view to a new form.

I have done some coding here but when i double click on the list view it shows the first data in the table in a new form. It does not show current selected row in a new form.

 public partial class Patient : Form
    {
 private string conString = "Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Tuscan21;Integrated Security=true";
        private DataSet dataset;
      private string selectString="Select p.patient_id as patient_id,p.patient_dob as patient_dob,n.patient_first_name as patient_fname,n.patient_middle_name as patient_mname,n.patient_last_name as patient_lname,p.patient_sex as patient_sex,n.virology as virology,a.apartment_name as apartment_name,a.door_number as door_number,a.street_name_1 as street_name_1,a.street_name_2 as street_name_2,a.street_name_3 as street_name_3,a.village as village,a.city as city,a.state as state,a.country as country,a.apartment_number as apartment_number,a.pincode as pincode from PATIENT_NAME n,PATIENT_ID p,ADDRESS a where n.patient_id=p.patient_id and a.patient_id=p.patient_id";

public void loadPatient(string mpatientID)
        {
            button1.Text = "Save";
            SqlConnection conn = new SqlConnection(conString);
            SqlDataAdapter dap = new SqlDataAdapter(selectString, conn);
            dataset = new DataSet();
            dap.Fill(dataset, "PATIENT_NAME");
            dap.Fill(dataset, "PATIENT_ID");
            dap.Fill(dataset, "ADDRESS");
            txtFname.DataBindings.Add("Text", dataset, "PATIENT_NAME.patient_fname");
            txtMname.DataBindings.Add("Text", dataset, "PATIENT_NAME.patient_mname");
            txtLname.DataBindings.Add("Text", dataset, "PATIENT_NAME.patient_lname");
            cboVirology.DataBindings.Add("Text", dataset, "PATIENT_NAME.virology");
            cboSex.DataBindings.Add("Text", dataset, "PATIENT_ID.patient_sex");
            dtDOB.DataBindings.Add("Text", dataset, "PATIENT_ID.patient_dob");
            txtApartmentNo.DataBindings.Add("Text", dataset, "ADDRESS.apartment_number");
            txtApartmentName.DataBindings.Add("Text", dataset, "ADDRESS.apartment_name");
            txtDoorNo.DataBindings.Add("Text", dataset, "ADDRESS.door_number");
            txtStreet1.DataBindings.Add("Text", dataset, "ADDRESS.street_name_1");
            txtStreet2.DataBindings.Add("Text", dataset, "ADDRESS.street_name_2");
            txtStreet3.DataBindings.Add("Text", dataset, "ADDRESS.street_name_3");
            txtVillageArea.DataBindings.Add("Text", dataset, "ADDRESS.village");
            txtCity.DataBindings.Add("Text", dataset, "ADDRESS.city");
            txtState.DataBindings.Add("Text", dataset, "ADDRESS.state");
            txtPostalCode.DataBindings.Add("Text", dataset, "ADDRESS.pincode");
            txtCountry.DataBindings.Add("Text", dataset, "ADDRESS.country");

        }

The above code is in my form(Patient) with method/function public void loadPatient(string mpatientID)

The below code is from my another form Searchpatient.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
namespace DRRS_in_Csharp
{
    public partial class Searchpatient : Form
    {
        public Searchpatient()
        {
            InitializeComponent();
            this.lstSearch.DoubleClick += new System.EventHandler(this.lstSearch_DoubleClick);
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Tuscan21;Integrated Security=true");
            lstSearch.Items.Clear();
            DataTable dt = new DataTable();
            SqlCommand cmd=new SqlCommand();
            DataSet ds;
            SqlDataAdapter adp = new SqlDataAdapter();
            lstSearch.Columns.Add("ID",100, HorizontalAlignment.Left);
            lstSearch.Columns.Add("DOB",95, HorizontalAlignment.Center);
            lstSearch.Columns.Add("NAME",180, HorizontalAlignment.Center);
            lstSearch.Columns.Add("SEX", 60, HorizontalAlignment.Right);
            lstSearch.Visible = true;
            lstSearch.View = View.Details;
            lstSearch.GridLines = true;
            if (txtPatientID.Text.Trim() != "")
            {
                cmd = new SqlCommand("Select p.patient_id as patient_id,p.patient_dob as patient_dob,n.patient_first_name+' '+n.patient_middle_name+' '+n.patient_last_name as patient_name,p.patient_sex as patient_sex from PATIENT_NAME n,PATIENT_ID p where p.patient_id=n.patient_id and n.patient_id=" + txtPatientID.Text.Trim() + "", conn);
                adp = new SqlDataAdapter(cmd);
                ds = new DataSet();
                adp.Fill(ds,"PATIENT_NAME"+"PATIENT_ID");
                dt = ds.Tables["PATIENT_NAME" + "PATIENT_ID"];
            }
            else if(txtName.Text.Trim() != "")
            {
                cmd = new SqlCommand("Select p.patient_id as patient_id,p.patient_dob as patient_dob,n.patient_first_name+' '+n.patient_middle_name+' '+n.patient_last_name as patient_name,p.patient_sex as patient_sex from PATIENT_NAME n,PATIENT_ID p where p.patient_id=n.patient_id and ((n.patient_first_name like '%" + (txtName.Text.Trim()) + "%') or (n.patient_middle_name like '%" + (txtName.Text.Trim()) + "%') or (n.patient_last_name like '%" + (txtName.Text.Trim()) + "%'))", conn);
                adp = new SqlDataAdapter(cmd);
                ds = new DataSet();
                adp.Fill(ds, "PATIENT_NAME" + "PATIENT_ID");
                dt = ds.Tables["PATIENT_NAME" + "PATIENT_ID"];
            }
            else if (dtDOB.Tag != null)
            {
                cmd = new SqlCommand("Select p.patient_id as patient_id,p.patient_dob as patient_dob,n.patient_first_name+' '+n.patient_middle_name+' '+n.patient_last_name as patient_name,p.patient_sex as patient_sex from PATIENT_NAME n,PATIENT_ID p where n.patient_id=p.patient_id and p.patient_dob="+dtDOB.Value+"", conn);
                adp = new SqlDataAdapter(cmd);
                ds = new DataSet();
                adp.Fill(ds, "PATIENT_NAME" + "PATIENT_ID");
                dt = ds.Tables["PATIENT_NAME" + "PATIENT_ID"];
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lstSearch.Items.Add(dt.Rows[i].ItemArray[0].ToString());
                lstSearch.Items[i].SubItems.Add(dt.Rows[i].ItemArray[1].ToString());
                lstSearch.Items[i].SubItems.Add(dt.Rows[i].ItemArray[2].ToString());
                lstSearch.Items[i].SubItems.Add(dt.Rows[i].ItemArray[3].ToString());

               

            }

        }


In the above code lstSearch is name of my listview. For list view double click event i have done some coding .

private void lstSearch_DoubleClick(object sender, EventArgs e)
        {
            string vFieldNameStr;
            vFieldNameStr ="";
            Patient p = new Patient();
            p.loadPatient(vFieldNameStr);
            p.Show();
        }

During run-time when i double click on my listview it shows first entry in my table in database.

Can anyone help me please to how to solve my problem. Any help would be appreciated.




Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>