C# как использовать таблицу из sqlserver show in listbox. При выборе из списка можно редактировать в datagridview.
private void button4_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(this.connetionString); string sql = @"SELECT * from amd_err;"; SqlDataAdapter sda = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); sda.Fill(dt); this.listBox1.DataSource = result; this.listBox1.DataSource = dt; listBox1.DisplayMember = "amd_err"; //DataSet tblData = new DataSet(); //string sql = @"SELECT * from amd_err;"; //SqlConnection conn = new SqlConnection(this.connetionString); //SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //sda.Fill(tblData, "amd_err"); //MessageBox.Show("Rows Count: !" ); //bindingSource1.DataSource = tblData; //dataGridView1.ReadOnly = true; //this.dataGridView1.DataSource = bindingSource1; //dataGridView1.DataMember = "amd_err"; //this.dataGridView1.Refresh();
Что я уже пробовал:
<pre>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace connectDatabase { public partial class Form1 : Form { private string DBUID; private string DBPWD; private string DBSVR; private string DBNAME; private string connetionString; public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void label4_Click(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { Application.Exit(); } private void bindingSource1_CurrentChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.DBSVR = DBSname.Text; this.DBPWD = DBPass.Text; this.DBUID = DBUname.Text; this.DBNAME = DBname.Text; SqlConnection cnn; connetionString = @"Data Source=" + this.DBSVR + ";Initial Catalog=" + this.DBNAME + ";User ID=" +this.DBUID+ ";Password="+ this.DBPWD+ ";"; cnn = new SqlConnection(connetionString); try { cnn.Open(); }catch (Exception ex) { MessageBox.Show("Connection Can't connect !" + ex.Message); return; } MessageBox.Show("Connection Open !"); cnn.Close(); } private void button4_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(this.connetionString); string sql = @"SELECT * from amd_err;"; SqlDataAdapter sda = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); sda.Fill(dt); this.listBox1.DataSource = result; this.listBox1.DataSource = dt; listBox1.DisplayMember = "amd_err"; //DataSet tblData = new DataSet(); //string sql = @"SELECT * from amd_err;"; //SqlConnection conn = new SqlConnection(this.connetionString); //SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //sda.Fill(tblData, "amd_err"); //MessageBox.Show("Rows Count: !" ); //bindingSource1.DataSource = tblData; //dataGridView1.ReadOnly = true; //this.dataGridView1.DataSource = bindingSource1; //dataGridView1.DataMember = "amd_err"; //this.dataGridView1.Refresh(); } } }
Sinisa Hajnal
В чем проблема с кодом? Какую ошибку вы получаете? - Куда же?
Gerry Schmitz
Один из них, вероятно, излишен.