Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi my name is vishal for 2 days i have been breaking my head on how to pass selected item of datas of listbox into listview of different column datas in c# windows forms with sql server 2008?. I have 2 list boxes in my form named:frmGetSystemData.
1st listbox name:lstIP,enabled:true,Multi Column:false,Selection Mode:One and visible:true.
2nd listbox name:lstDialyzerID,enabled:true,Multi Column:false,Selection Mode:One and visible:true
i have one button control named:btnUpdateDialyzer,enabled:true,text:Start Dialyzer Cleaning Process and visible:true.
i have a listview control in same form named:lvwConnections,enabled:true,FullRowSelect:true,GridLines:true,MultiSelect:false,visible:true and View:Details.

Given below is my c# code on how i populate my listboxes(lstDialyzerID) and another listbox(lstIP) in form(frmGetSystemData):
C#
private void frmGetSystemData_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            lstIP.Items.Clear();
            SqlCommand hcd = new SqlCommand();
            DataSet ms;
            DataTable mt = new DataTable();
            SqlDataAdapter mju = new SqlDataAdapter();
            hcd = new SqlCommand("Select ManufacturerName,DCS_ip_address from Reprocess", conn);
            mju = new SqlDataAdapter(hcd);
            ms = new DataSet();
            mju.Fill(ms, "Reprocess");
            mt = ms.Tables["Reprocess"];
            for(int j=0;j<mt.Rows.Count;j++)
            {
                MDIParent1 g=new MDIParent1();
                if(mt.Rows[j].ItemArray[0].ToString()==g.sckClient.RemoteHostIP)
                {
                    lstIP.Items.Add(mt.Rows[j].ItemArray[0].ToString()+"-"+mt.Rows[j].ItemArray[1].ToString());
                }
            }
            lstDialyzerID.Items.Clear();
            SqlCommand cmd = new SqlCommand();
            DataSet ds;
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter();
            cmd = new SqlCommand("Select d.dialyserID,pn.patient_id,pn.patient_first_name,pn.patient_last_name from dialyser d,patient_name pn where d.closed_status=0 and d.deleted_status=0 and pn.patient_id=d.patient_id and pn.status=1", conn);
            adp = new SqlDataAdapter(cmd);
            ds = new DataSet();
            adp.Fill(ds, "dialyser");
            adp.Fill(ds, "patient_name");
            dt = ds.Tables["dialyser"];
            dt = ds.Tables["patient_name"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lstDialyzerID.Items.Add(dt.Rows[i].ItemArray[0].ToString() + "|" + dt.Rows[i].ItemArray[2].ToString() + " " + dt.Rows[i].ItemArray[3].ToString() + "-" + string.Format("000" + dt.Rows[i].ItemArray[1].ToString()));
            }
        }

As you can see i have ManufacturerName and DCS_ip_address datas from table(Reprocess) from sql server into listbox(lstIP) in c# windows forms. I also have dialyserID,patient_first_name and patient_last_name from tables(dialyser,patient_name) from sql server into listbox(lstDialyzerID) in c# windows forms.

What i want is after selecting an item from lstIP(listbox) and lstDialyzerID(listbox) and when i click button(btnUpdateDialyzer) in my form(frmGetSystemData) i want datas of ManufacturerName from lstIP,dialyserID,patient first name and patient last name from lstDialyzerID to appear in listview(lvwConnections) with column name DCS System containing datas of ManufacturerName(from lstIP),DialyzerID containing data of dialyserID(from lstDialyzerID) and Patient Name containing datas of patient first name and patient last name(from lstDialyzerID).

Tell me how should i do to achieve my required output from btnUpdateDialyzer(button) in my form:frmGetSystemData. Can anyone help me/guide me please?! Tell me what modifications must i do in my button(btnUpdateDialyzer) in form(frmGetSystemData) to achieve my required output? Can anyone help me please?! Any help/guidance in solving of this problem would be greatly appreciated!
Posted
Updated 17-Jun-14 1:15am
v2
Comments
ZurdoDev 17-Jun-14 15:47pm    
This is very hard to read. Can you explain clearly what your question is?
j snooze 17-Jun-14 17:16pm    
RyanDev is correct, if I was grading the code I would give it a low score looks like a big mash of everything you want to do all in one event method...but I'm not here to critique(even though I did). My suggestion is either create some classes, like a manufacturer class and a patient class with all their properties Manufacturer(IP, Name) Patient(ID, firstname, lastname etc....) then with your data create a List<t> of those objects, or basically an enumerable collection and bind this to your listbox. The beauty of this is, cleaner looking code, and you can bind objects, while only displaying the user friendly needed data, but still get at all the properties of the selected item. If you don't like that route, you can always just bind the listview to the datatables and reference fields. YOu might be able to get at the datarow of the selected item. Just suggestions.
Member 10248768 18-Jun-14 0:50am    
Thanks for replying to my query on such short notice Mr.RyanDev
Ok i have 2 list boxes in my form(frmGetSystemData).
So 1st listbox named:lstIP displays ManufacturerName along with DCS_ip_address
2nd listbox named:lstDialyzerID displays dialyserID along with patient_first_name,patient_last_name along with patient_id

What i want is after selecting an item from both lstIP and lstDialyzerID when i click on btnUpdateDialyzer(button) i want ManufacturerName from lstIP,dialyserID,patient_first_name,patient_last_name along with patient_id from lstDialyzerID.i want these to be displayed/datas binded into listview(lvwConnections) in c# windows forms.
That is what i want! Tell me/guide me on how to achieve this required result? I have browsed through net regarding finding solution to this problem but in vain and with no success. Can anyone help me/guide me please on how to achieve this required result?!

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