本文介绍了通过从c#中的组合框中选择,在dgv中显示sql db中的多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这里是用于在sql db中插入新值的代码。现在我想要帮助如何在c#中使用组合框显示来自相同sql db的多个表...就像当我从下拉列表中选择任何表时列表表格显示在数据网格视图中 - 命名空间 insertdb { public 部分 类 Form1:表格 { List< int> lstNewRows = new List< int>(); public Form1() { InitializeComponent(); } 私人 void Form1_Load( object sender,EventArgs e) { String strConnection = 数据源= HP \\SQLEXPRESS; database = MK; Integrated Security = true; SqlConnection con = new SqlConnection(strConnection); 尝试 { con.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = con; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = 选择*来自lol; SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd); DataTable dtRecord = new DataTable(); sqlDataAdap.Fill(dtRecord); dataGridView1.DataSource = dtRecord; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } 私有 void InsertInfo() { string connectionString = null ; SqlConnection连接; SqlDataAdapter adapter = new SqlDataAdapter(); string value1 = ; string value2 = ; connectionString = @ 数据源= HP \SQLEXPRESS;数据库= MK;集成安全性=真; connection = new SqlConnection(connectionString); foreach ( int rowIndex in lstNewRows) { if (dataGridView1。行[rowIndex] .Cells [ 0 ]。值!= null && dataGridView1.Rows [ rowIndex] .Cells [ 1 ]。值!= null ) { value1 = dataGridView1.Rows [rowIndex] .Cells [ 0 ]。Value.ToString(); value2 = dataGridView1.Rows [rowIndex] .Cells [ 1 ]。Value.ToString(); string sql = 插入lol(名称,标记)值(' + value1 + ',' + value2 + '); 尝试 { connection.Open(); adapter.InsertCommand = new SqlCommand(sql,connection); adapter.InsertCommand.ExecuteNonQuery(); MessageBox.Show( 插入行!!); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } private void INSERT_Click( object sender ,EventArgs e) { InsertInfo(); } private void dataGridView1_DefaultValuesNeeded( object sender,DataGridViewRowEventArgs e) { lstNewRows.Add(e.Row.Index); } } } 解决方案 要做的步骤: 1)将Form_Load过程的主体移动到另一个带有一个输入参数的过程中,如下所示: 私有 void ChangeDGVSource( String sTableName) { // 您的代码 // .... // 您只需更改此行: sqlCmd.CommandText = 从 + sTableName;中选择*; // ... // 下一部分代码 } 2)手动将sdd项目复制到组合框中或从数据库中获取数据(与dgv相同)并设置 ComboBox [ ^ ]。使用 3)添加按钮并使用 OnClick 事件来调用 ChangeDGVSource here is the code i hv used for inserting new values in sql db..now i want help in how to show multiple tables from same sql db in c# using combo box...as in when i select any table from dropdown list the table is shown in data grid view- namespace insertdb { public partial class Form1 : Form { List<int> lstNewRows = new List<int>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true"; SqlConnection con = new SqlConnection(strConnection); try { con.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = con; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "Select * from lol"; SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd); DataTable dtRecord = new DataTable(); sqlDataAdap.Fill(dtRecord); dataGridView1.DataSource = dtRecord; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void InsertInfo() { string connectionString = null; SqlConnection connection; SqlDataAdapter adapter = new SqlDataAdapter(); string value1 = ""; string value2 = ""; connectionString = @"Data Source=HP\SQLEXPRESS;database=MK;Integrated Security=true"; connection = new SqlConnection(connectionString); foreach (int rowIndex in lstNewRows) { if (dataGridView1.Rows[rowIndex].Cells[0].Value != null && dataGridView1.Rows[rowIndex].Cells[1].Value != null) { value1 = dataGridView1.Rows[rowIndex].Cells[0].Value.ToString(); value2 = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString(); string sql = "insert into lol (name,marks) values('" + value1 + "','" + value2 + "')"; try { connection.Open(); adapter.InsertCommand = new SqlCommand(sql, connection); adapter.InsertCommand.ExecuteNonQuery(); MessageBox.Show("Row inserted !! "); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } private void INSERT_Click(object sender, EventArgs e) { InsertInfo(); } private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) { lstNewRows.Add(e.Row.Index); } } } 解决方案 Steps to do:1) move the body of Form_Load procedure into another procedure with one input parameter, like this:Private void ChangeDGVSource(String sTableName){//your code here//....//you need to change only this line: sqlCmd.CommandText = "Select * from " + sTableName;// ...// next part of code }2) Sdd items to combobox manually or fetch the data from your database (in the same way you do it for dgv) and set the source of ComboBox[^]. Use 3) Add button and use OnClick event to call ChangeDGVSource 这篇关于通过从c#中的组合框中选择,在dgv中显示sql db中的多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 01:08