本文介绍了如何使用checkedListBox中的多选项将多个项检索到dataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好吧, i已经坚持了这一步,所以如果你能伸出手就会非常感激。 i希望从sql到dataGridView检索数据选择 for 示例我想要dataGridView显示数据名称是('sam','jole') 和im使用checkedListBox进行选择但是它只检索第一个选择 这是我的代码 private void button1_Click( object sender,EventArgs e) { List< string> oranad = new List< string>(); foreach ( string sm in checkedListBox1.Items) { cn.Open(); string 选择 = select * from productvw where firstname in(@firstname); SqlDataAdapter dataAdapter = new SqlDataAdapter( select ,cn); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); dataAdapter.SelectCommand.Parameters.Add( @ firstname,sm); DataTable ds = new DataTable(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true ; dataGridView1.DataSource = ds; cn.Close(); 这些代码只检索第一个选择到datagridview。解决方案 尝试如下: private void button1_Click( object sender,EventArgs e) { List< string> oranad = new List< string>(); StringBuilder sb = new StringBuilder( string .Empty); foreach (ListItem item in checkedListBox1.Items) { if (item.Selected) { sb.Append(item.Value + ,); } } cn.Open(); string 选择 = select * from productvw where firstname in(@firstname); SqlDataAdapter dataAdapter = new SqlDataAdapter( select ,cn); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); dataAdapter.SelectCommand.Parameters.Add( @ firstname,sb.ToString() .TrimEnd(' ,')); DataTable ds = new DataTable(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true ; dataGridView1.DataSource = ds; cn.Close(); } 这里循环遍历所有选定的项目并在StringBuilder中生成名称。而不是执行SQL连接,我只使用过一次。 如果要检索 firstname 列等于的记录任何选定的值,那么你需要将多个参数传递给查询。 有传递多个值的各种方法 [ ^ ]为单个参数。或者,您可以使用多个参数: private void button1_Click( object sender,EventArgs e) { using (SqlConnection connection = new SqlConnection( 您的连接字符串在这里)) 使用(SqlCommand command = new SqlCommand( string .Empty,connection)) { StringBuilder query = new StringBuilder( SELECT * FROM productvw); foreach (ListItem item in checkedListBox1.Items) { if (item.Selected) { if (command.Parameters.Count == 0 ) { query.Append( WHERE名字IN(); } else { query.Append( ,); } string name = @ p + command.Parameters.Count; command.Parameters.AddWithValue(name,ite m.Value); query.Append(name); } } if (command.Parameters.Count!= 0 ) { query.Append(' )'); } command.CommandText = query.ToString(); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataTable ds = new DataTable(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true ; dataGridView1.DataSource = ds; } } 嗨每一个...... 所以,谢谢你回答你真的帮助并让我学到新东西谢谢 尝试和尝试后 创立了解决方案 私有 void button1_Click( object sender,EventArgs e) { 使用(SqlCommand command = new SqlCommand( string .Empty,cn)) { bool first = 真; StringBuilder query = new StringBuilder( SELECT * FROM productvw); foreach ( string item in checkedListBox1.CheckedItems) { if (first) { if (command.Parameters.Count == 0 ) { query.Append( WHERE名字IN(); } else { query.Append( ,); } string name = @ p + command.Parameters.Count; command.Parameters.AddWithValue(name,item.ToString()); query.Append(name); } } if (command.Parameters.Count!= 0 ) { query.Append(' )'); } command.CommandText = query.ToString(); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataTable ds = new DataTable(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true ; dataGridView1.DataSource = ds; } hi everyone,,i have stucked in this steps so it will be so appreciated if you could give hand,i want to retrieve data from sql to dataGridView with multi selection for example i want dataGridView show data where name is ('sam','jole')and im using checkedListBox for selecting but it only retrieve the first selectionand this is my codeprivate void button1_Click(object sender, EventArgs e) { List<string> oranad = new List<string>() ; foreach (string sm in checkedListBox1.Items) { cn.Open(); string select = "select * from productvw where firstname in(@firstname)"; SqlDataAdapter dataAdapter = new SqlDataAdapter(select, cn); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); dataAdapter.SelectCommand.Parameters.Add("@firstname",sm); DataTable ds = new DataTable(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true; dataGridView1.DataSource = ds; cn.Close();those code only retrieve the first selection to datagridview . 解决方案 Try like below:private void button1_Click(object sender, EventArgs e){List<string> oranad = new List<string>() ;StringBuilder sb = new StringBuilder(string.Empty);foreach (ListItem item in checkedListBox1.Items){if (item.Selected) {sb.Append(item.Value + ","); }}cn.Open();string select = "select * from productvw where firstname in(@firstname)";SqlDataAdapter dataAdapter = new SqlDataAdapter(select, cn); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);dataAdapter.SelectCommand.Parameters.Add("@firstname",sb.ToString().TrimEnd(','));DataTable ds = new DataTable();dataAdapter.Fill(ds);dataGridView1.ReadOnly = true;dataGridView1.DataSource = ds;cn.Close();}Here it is looping over all selected items and generating names in StringBuilder. Instead of executing the SQL Connection, I used once only.If you want to retrieve the records where the firstname column is equal to any of the selected values, then you need to pass multiple parameters to the query.There are various ways to pass multiple values[^] to a single parameter. Alternatively, you could use multiple parameters:private void button1_Click(object sender, EventArgs e){ using (SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")) using (SqlCommand command = new SqlCommand(string.Empty, connection)) { StringBuilder query = new StringBuilder("SELECT * FROM productvw"); foreach (ListItem item in checkedListBox1.Items) { if (item.Selected) { if (command.Parameters.Count == 0) { query.Append("WHERE firstname IN ("); } else { query.Append(", "); } string name = "@p" + command.Parameters.Count; command.Parameters.AddWithValue(name, item.Value); query.Append(name); } } if (command.Parameters.Count != 0) { query.Append(')'); } command.CommandText = query.ToString(); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataTable ds = new DataTable(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true; dataGridView1.DataSource = ds; }}hi every one ... so thanks for you answer guys you really helped and made me learn new thing thanks after trying and trying founded the solutionprivate void button1_Click(object sender, EventArgs e) { using (SqlCommand command = new SqlCommand(string.Empty,cn)) { bool first = true; StringBuilder query = new StringBuilder("SELECT * FROM productvw "); foreach (string item in checkedListBox1.CheckedItems) { if (first) { if (command.Parameters.Count == 0) { query.Append("WHERE firstname IN ("); } else { query.Append(", "); } string name = "@p" + command.Parameters.Count; command.Parameters.AddWithValue(name, item.ToString()); query.Append(name); } } if (command.Parameters.Count != 0) { query.Append(')'); } command.CommandText = query.ToString(); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataTable ds = new DataTable(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true; dataGridView1.DataSource = ds; } 这篇关于如何使用checkedListBox中的多选项将多个项检索到dataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 23:31