JEtheDBA JEtheDBA - 4 days ago 5
C# Question

C# Display All Members of a List

I have a WCF service and a Windows Form. When the button on the Form is clicked, I want a List (with 4 Members) populated via the Service. I then want the List displayed on the From with the 4 Members as Columns.

Better yet, I would like to display use each Member as part of a textBox.

Right now, I can only display each Member in a separate listBox.

Here is the code. GateInfo.cs:

using System.Runtime.Serialization;

namespace JimWcfFormTest3
{
[DataContract]

public class GateInfo
{
[DataMember]
public int carid { get; set; }

[DataMember]
public int paid_at_gate { get; set; }

[DataMember]
public int wash_pkg_purch { get; set; }

[DataMember]
public string carte { get; set; }
}
}


Service1.cs:

using System.Collections.Generic;
using System.ServiceModel;

namespace JimWcfFormTest3
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
private List<GateInfo> _gate;

private Service1()
{
_gate = new List<GateInfo>();
}

public void Gate_to_Server(GateInfo gatein)
{
if (gatein != null) _gate.Add(gatein);
}

public List<GateInfo> Server_to_Term()
{
return _gate;
}
}
}


Form1.cs:

private void button1_Click(object sender, EventArgs e)
{
int carnum = 2;
int pay = 1;
int wash = 5;
string txt = "TEST";
var data_out = new GateInfo { carid = carnum, paid_at_gate = pay, wash_pkg_purch = wash, carte = txt };

server.Gate_to_Server(data_out);

listBox1.DisplayMember = "carid";
listBox1.DataSource = server.Server_to_Term();

listBox4.DisplayMember = "paid_at_gate";
listBox4.DataSource = server.Server_to_Term();

listBox5.DisplayMember = "wash_pkg_purch";
listBox5.DataSource = server.Server_to_Term();

listBox6.DisplayMember = "carte";
listBox6.DataSource = server.Server_to_Term();
}


Ideally, I would like to display this all in one textBox like so:

"timestamp: Car=carid, Pay=paid_at_gate, Package = wash_pkg_purch, Ala Carte=carte"

Answer

@RezaAghaei answered the initial question:

Drop a DataGridView on form and then set a list as DataSource of the control, for example dataGridView1.DataSource = server.Server_to_Term()

The question of how to do put this all in a text box is answered here: How to Override ToString and Display in a textBox

Comments