EDN Admin
Well-known member
<span style="font-family:Times New Roman,serif Hey everyone!<span>
I’m trying to learn C# better.<span> I know some things, but obviously not enough, because I just got stuck on a new project.<span>
Basically, I am trying to load data into a ListBox (works fine).<span>
Then, I want to be able to select multiple items from the list box, and display these in a DataTable (not working fine).<span>
Below is my code.<span> I think I am close, but I can’t seem to get this working for some reason.
<span style="font-family:Times New Roman,serif
using System;<br/>
using System.Collections.Generic;<br/>
using System.ComponentModel;<br/>
using System.Data;<br/>
using System.Drawing;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Windows.Forms;<br/>
using System.Data.SqlClient;<br/>
<br/>
<br/>
namespace WindowsFormsApplication1<br/>
<br/>
{<br/>
public partial class Form1 : Form<br/>
{<br/>
public Form1()<br/>
{<br/>
InitializeComponent();<br/>
}<br/>
<br/>
<br/>
private void button1_Click(object sender, EventArgs e)<br/>
{<br/>
<br/>
DataSet ds = new DataSet();<br/>
SqlDataAdapter daOrders = new SqlDataAdapter();<br/>
SqlDataAdapter daOrder_Details = new SqlDataAdapter();<br/>
<br/>
SqlConnection cs = new SqlConnection("Data Source=EXCEL-PC;Initial Catalog=NORTHWIND.MDF;Integrated Security=True");<br/>
SqlDataAdapter da = new SqlDataAdapter("Select Distinct CompanyName From Customers Order By CompanyName", cs);<br/>
<br/>
<br/>
DataTable dt = new DataTable();<br/>
da.Fill(dt);<br/>
<br/>
for (int i = 0; i < listBox1.SelectedItems.Count; i++)<br/>
{<br/>
string item = listBox1.SelectedItems.ToString();<br/>
ds.Tables["Customers"].DefaultView.RowFilter = "CustomerID = " + listBox1.SelectedItems;<br/>
dataGridView1.DataSource = ds.Tables["Customers"];<br/>
}<br/>
}<br/>
<br/>
<br/>
private void Form1_Load(object sender, EventArgs e)<br/>
{<br/>
// TODO: This line of code loads data into the _NORTHWIND_MDFDataSet.Customers table. You can move, or remove it, as needed.<br/>
this.customersTableAdapter.Fill(this._NORTHWIND_MDFDataSet.Customers);<br/>
SqlConnection cs = new SqlConnection("Data Source=EXCEL-PC;Initial Catalog=NORTHWIND.MDF;Integrated Security=True");<br/>
SqlDataAdapter da = new SqlDataAdapter("Select Distinct CompanyName From Customers Order By CompanyName", cs);<br/>
<br/>
DataTable dt = new DataTable();<br/>
da.Fill(dt);<br/>
<br/>
for (int i = 0; i < dt.Rows.Count; i++)<br/>
{<br/>
listBox1.Items.Add(dt.Rows["CompanyName"]);<br/>
}<br/>
<br/>
}<br/>
}<br/>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif
<span style="font-family:Times New Roman,serif
<
Ryan Shuell
<br/>
<br/>
View the full article
I’m trying to learn C# better.<span> I know some things, but obviously not enough, because I just got stuck on a new project.<span>
Basically, I am trying to load data into a ListBox (works fine).<span>
Then, I want to be able to select multiple items from the list box, and display these in a DataTable (not working fine).<span>
Below is my code.<span> I think I am close, but I can’t seem to get this working for some reason.
<span style="font-family:Times New Roman,serif
using System;<br/>
using System.Collections.Generic;<br/>
using System.ComponentModel;<br/>
using System.Data;<br/>
using System.Drawing;<br/>
using System.Linq;<br/>
using System.Text;<br/>
using System.Windows.Forms;<br/>
using System.Data.SqlClient;<br/>
<br/>
<br/>
namespace WindowsFormsApplication1<br/>
<br/>
{<br/>
public partial class Form1 : Form<br/>
{<br/>
public Form1()<br/>
{<br/>
InitializeComponent();<br/>
}<br/>
<br/>
<br/>
private void button1_Click(object sender, EventArgs e)<br/>
{<br/>
<br/>
DataSet ds = new DataSet();<br/>
SqlDataAdapter daOrders = new SqlDataAdapter();<br/>
SqlDataAdapter daOrder_Details = new SqlDataAdapter();<br/>
<br/>
SqlConnection cs = new SqlConnection("Data Source=EXCEL-PC;Initial Catalog=NORTHWIND.MDF;Integrated Security=True");<br/>
SqlDataAdapter da = new SqlDataAdapter("Select Distinct CompanyName From Customers Order By CompanyName", cs);<br/>
<br/>
<br/>
DataTable dt = new DataTable();<br/>
da.Fill(dt);<br/>
<br/>
for (int i = 0; i < listBox1.SelectedItems.Count; i++)<br/>
{<br/>
string item = listBox1.SelectedItems.ToString();<br/>
ds.Tables["Customers"].DefaultView.RowFilter = "CustomerID = " + listBox1.SelectedItems;<br/>
dataGridView1.DataSource = ds.Tables["Customers"];<br/>
}<br/>
}<br/>
<br/>
<br/>
private void Form1_Load(object sender, EventArgs e)<br/>
{<br/>
// TODO: This line of code loads data into the _NORTHWIND_MDFDataSet.Customers table. You can move, or remove it, as needed.<br/>
this.customersTableAdapter.Fill(this._NORTHWIND_MDFDataSet.Customers);<br/>
SqlConnection cs = new SqlConnection("Data Source=EXCEL-PC;Initial Catalog=NORTHWIND.MDF;Integrated Security=True");<br/>
SqlDataAdapter da = new SqlDataAdapter("Select Distinct CompanyName From Customers Order By CompanyName", cs);<br/>
<br/>
DataTable dt = new DataTable();<br/>
da.Fill(dt);<br/>
<br/>
for (int i = 0; i < dt.Rows.Count; i++)<br/>
{<br/>
listBox1.Items.Add(dt.Rows["CompanyName"]);<br/>
}<br/>
<br/>
}<br/>
}<br/>
}
<p style="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none
<span style="font-family:Times New Roman,serif
<span style="font-family:Times New Roman,serif
<
Ryan Shuell
<br/>
<br/>
View the full article