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

how to bind selected items of 2 listboxes into a single listview in c# windows forms

$
0
0

Hi my name is vishal for past 10 days i have been breaking my head on how to bind selected items from 2 listboxes into a single listview in c# windows forms.

I am on transition from vb6 adodb with Ms access to c# windows forms with sql server 2008.

I have a form named:frmGetSystemData in it i have 2 listboxes

1st listbox named:lstIP,enabled:true,MultiColumn:False,SelectionMode:One and visible:true

2nd listbox named:lstDialyzerID,enabled:true,MultiColumn:False,SelectionMode:One and visible:true

I populate my lstIP(listbox control) in form load event through followingc# code with sql server2008:

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();
            }
            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()));
            }
        }

The above code executes well with no problem at all!

I am also able to populate my lstDialyzerID(listbox control) in form load event through followingc# code with sql server2008:

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.Net;
using System.Net.Sockets;
namespace DRRS_CSharp
{
    public partial class frmGetSystemData : Form
    {

Socket sck;
        EndPoint epLocal;

public frmGetSystemData()
        {
            InitializeComponent();
           sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);  
        }

private string GetLocalIP()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            return "127.0.0.1";
        }

private void frmGetSystemData_Load(object sender, EventArgs e) { frmGetSystemData g = new frmGetSystemData(); SqlConnection cont = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true"); if (cont.State != ConnectionState.Open) { cont.Open(); } g.lstIP.Items.Clear(); SqlCommand mdr = new SqlCommand(); DataSet dr; DataTable ht = new DataTable(); SqlDataAdapter awc = new SqlDataAdapter(); mdr = new SqlCommand("Select r.ManufacturerName,r.DCS_ip_address from Reprocess r where r.DCS_ip_address='" + GetLocalIP() + "'", cont); awc = new SqlDataAdapter(mdr); dr = new DataSet(); awc.Fill(dr, "Reprocess"); ht = dr.Tables["Reprocess"]; for (int j = 0; j < ht.Rows.Count; j++) { g.lstIP.Items.Add(ht.Rows[j].ItemArray[0].ToString() + "-" + ht.Rows[j].ItemArray[1].ToString()); } g.Show(); }

The above code also executes well with no problem at all!

Given below is the structure of my table named:Reprocess in sql server 2008

table  name:Reprocess

Column Name                              DataType                            AllowNulls

ManufacturerName                       nvarchar(50)                           Yes

MFR_SL_No                                   nvarchar(50)                           Yes

row_upd_date                               datetime                                 Yes

volume                                          nvarchar(10)                            Yes

user_id                                             Int                                        Yes

packed_volume                                Int                                         Yes

DCS_ip_address                            nvarchar(15)                           Yes

agn                                                Int(primary key)                       No           

I have a listview in my form named:frmGetSystemData

listview name:lvwConnections,Activation:OneClick,CheckBoxes:False,Dock:Bottom,Enabled:true,FullRowSelect:true,Gridlines:true,HideSelection:true,HotTracking:true,HoverSelection:true,MultiSelect:false,View:Details and Visible:true.

i have a button in frmGetSystemData named:btnUpdateDialyzer,enabled:true,text:StartDialyzerCleaningProcess and visible:true.

What i want is when i click btnUpdateDialyzer i want selected item of lstIP(listbox) and want onlyManufacturerName(a field from table:Reprocess in sql server2008) fromlstIP,selected item of lstDialyzerID which containsdialyserID(a field from table:dialyser in sql server2008),patient_first_name,patient_last_name andpatient_id(fields from table:patient_name in sql server2008) to be binded tolvwConnections(listview) in form of separate columns in lvwConnections.  Is it possible?

I did some research on binding  selected items of 2 listboxes into a single listview and found out a code that did the trick which was what i needed but unfortunately it was invb6 code:

Given below is vb6 code:

Private Sub cmdUpdateDialyzer_Click()
Dim lst As ListItem
Dim ipAddress As String
Set lst = MDIForm1.lvwConnections.ListItems.Add(, ipAddress, getMachineName(ipAddress))
lst.SubItems(1) = Left(frmGetSystemData.lstDialyzerID.Text, InStr(1, frmGetSystemData.lstDialyzerID.Text, "|", vbTextCompare) - 1)
        lst.SubItems(2) = Mid(frmGetSystemData.lstDialyzerID.Text, InStr(1, frmGetSystemData.lstDialyzerID.Text, "|", vbTextCompare) + 1)
        lst.SubItems(3) = "Cleaning in progress..."
}
Function getMachineName(ipLocal As String) As String
    On Error GoTo errh
    Dim vSQLStr As String
    Dim oRS As New ADODB.Recordset
    Dim returnStr As String
    returnStr = ipLocal
    vSQLStr = "select ManufacturerName from reprocess where DCS_ip_address='" & ipLocal & "'"
    oRS.Open vSQLStr, adoDatabase, adOpenForwardOnly, adLockReadOnly
    Do While Not oRS.EOF
         '// Do something with the data'
         If (oRS.Fields("ManufacturerName").Value <> "") Then
            returnStr = oRS.Fields("ManufacturerName").Value
            Exit Do
         End If
         oRS.MoveNext
    Loop
    oRS.Close
    getMachineName = returnStr
    Exit Function
errh:
    getMachineName = ipLocal
End Function

I know that i may be asking too much help from this forum but i dont have anyone to guide me/help me solve this problem and that is the reason i am asking here. I want to tell this that socket classes in c# windows forms has nothing to do with binding selected items from 2 listboxes into single listview in c# windows forms.

Can anyone help me please.! Can anyone help me/guide me to solve this problem? Any help/guidance in solving of this problem would be greatly appreciated!

                     


vishal


Viewing all articles
Browse latest Browse all 2535

Trending Articles



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