C# C从dataGridView1填充ComboBox1
我有这个代码,在最后两行用6填充TXTACFELDSA。选定行中的列C# C从dataGridView1填充ComboBox1,c#,C#,我有这个代码,在最后两行用6填充TXTACFELDSA。选定行中的列 我想要ComboBox1上的一些东西。当我选择ComboBox1中填充6的某一行时。柱这可能吗?根据文本框进行操作 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; u
我想要ComboBox1上的一些东西。当我选择ComboBox1中填充6的某一行时。柱这可能吗?根据文本框进行操作
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ArsCRM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=test\s08r2;Initial Catalog=Demo;User id=sa;Password=123456";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(@"
SELECT acSubject, acAddress, acPost, acName, acPhone,
acFieldSA, acFieldSB, acFieldSC, acFieldSD, acFieldSE,
anFieldNA, anFieldNB, anFieldNC, anFieldND, anFieldNE, OdgovornaOsoba, acSubjTypeBuyer
FROM ARS.dbo._ARSCRM_vSubjekti order by acSubject
", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtSearch.Text))
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Empty;
}
else
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("acSubject like '%{0}%'", txtSearch.Text);
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.SelectedCells.Count > 0)
txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[5].Value.ToString();
}
}
}
或者,如果要将其作为项目添加到组合框中:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.SelectedCells.Count > 0)
{
txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[5].Value.ToString();
ComboBox1.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[6].Value.ToString();
}
}
这个问题是不可理解的,你能重写这个问题吗?请重新写这个问题,因为它没有意义。埃罗马诺已经解决了这个问题。我需要在ComboBox1中填入6。dataGridView1中选定行中的列。如果没有选定的单元格,您认为此代码会发生什么情况?另外,请重新格式化它,以便人们能够真正阅读它。这与问题代码中最后一个函数的1:1副本有什么不同吗?你是对的。我忘了括号。但你为什么给我命令。您可以自己重新格式化代码。非常感谢。有代码可以将单元格内容添加到组合框中。这就是区别-谢谢你,罗曼诺,这就是我需要的。就这么简单。对我来说,下一步就是将这个ComboBox1连接到另一个数据。
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.SelectedCells.Count > 0)
{
txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[5].Value.ToString();
ComboBox1.Items.Add(dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[6].Value.ToString();)
}
}