C# SqlServer中使用ComboBox和TextBox进行过滤
Filter with ComboBox and TextBox in C # SqlServer
我必须使用文本视图和组合框过滤数据网格的数据。但是,会发生以下情况:组合初始化为 0,因此它不会捕获第一项的值。尝试手动添加一个项目,使值 0 为 "Select",其他来自数据库。
错误:
System.ArgumentException: 'The Items collection cannot be modified
when the DataSource property is set.'
在以下方法中,我尝试制定 sql 查询以获取参数(组合框和文本框)并在数据网格中执行过滤器:
private void filtrarTituloYAEMP(int valor)
{
cmbTipoPago.Items.Add("Seleccione");
if (txtTitulo.Text == null || txtTitulo.Text == "")
{
try
{
llenaTitulo();
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
try
{
SqlConnection conexion = new SqlConnection();
conexion.ConnectionString = "acá mi conexión a la bd";
conexion.Open();
DataSet ds = new DataSet();
string sSQL = "SELECT titulo FROM V_CuetaWeb, ArchivoElectronico_MedioPago where titulo LIKE @valor + '%' and AEMP_Id = @id";
SqlCommand command = new SqlCommand(sSQL, conexion);
command.Parameters.Add("@valor", SqlDbType.VarChar).Value = valor;
command.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(cmbTipoPago.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds, "V_CuetaWeb");
conexion.Close();
dtgTitulo.DataSource = ds;
dtgTitulo.DataMember = "V_CuetaWeb";
}
catch (SqlException exx)
{
MessageBox.Show("Error: " + exx.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
显然,它不是那样工作的。所以,问题是:
1- ComboBox 有一个额外的选项 ('Select'),其值为 0
2- 使用文本框和组合框过滤的方法
有人知道如何修复这些细节吗?
可能最简单的方法是修改您的 Select 声明:
string sSQL = "SELECT titulo FROM V_CuetaWeb, ArchivoElectronico_MedioPago where titulo LIKE @valor + '%' and AEMP_Id = @id";
至
string sSQL = "select 'Seleccione' union SELECT titulo FROM V_CuetaWeb, ArchivoElectronico_MedioPago where titulo LIKE @valor + '%' and AEMP_Id = @id";
一旦查询 运行 并由 DataAdapter 填充,您还可以在结果数据表的位置 0 插入一个新的 DataRow。
ds.Tables[0].Rows.InsertAt(new DataRow("Seleccione"]),0);
我必须使用文本视图和组合框过滤数据网格的数据。但是,会发生以下情况:组合初始化为 0,因此它不会捕获第一项的值。尝试手动添加一个项目,使值 0 为 "Select",其他来自数据库。
错误:
System.ArgumentException: 'The Items collection cannot be modified when the DataSource property is set.'
在以下方法中,我尝试制定 sql 查询以获取参数(组合框和文本框)并在数据网格中执行过滤器:
private void filtrarTituloYAEMP(int valor)
{
cmbTipoPago.Items.Add("Seleccione");
if (txtTitulo.Text == null || txtTitulo.Text == "")
{
try
{
llenaTitulo();
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
try
{
SqlConnection conexion = new SqlConnection();
conexion.ConnectionString = "acá mi conexión a la bd";
conexion.Open();
DataSet ds = new DataSet();
string sSQL = "SELECT titulo FROM V_CuetaWeb, ArchivoElectronico_MedioPago where titulo LIKE @valor + '%' and AEMP_Id = @id";
SqlCommand command = new SqlCommand(sSQL, conexion);
command.Parameters.Add("@valor", SqlDbType.VarChar).Value = valor;
command.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(cmbTipoPago.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds, "V_CuetaWeb");
conexion.Close();
dtgTitulo.DataSource = ds;
dtgTitulo.DataMember = "V_CuetaWeb";
}
catch (SqlException exx)
{
MessageBox.Show("Error: " + exx.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
显然,它不是那样工作的。所以,问题是:
1- ComboBox 有一个额外的选项 ('Select'),其值为 0
2- 使用文本框和组合框过滤的方法
有人知道如何修复这些细节吗?
可能最简单的方法是修改您的 Select 声明:
string sSQL = "SELECT titulo FROM V_CuetaWeb, ArchivoElectronico_MedioPago where titulo LIKE @valor + '%' and AEMP_Id = @id";
至
string sSQL = "select 'Seleccione' union SELECT titulo FROM V_CuetaWeb, ArchivoElectronico_MedioPago where titulo LIKE @valor + '%' and AEMP_Id = @id";
一旦查询 运行 并由 DataAdapter 填充,您还可以在结果数据表的位置 0 插入一个新的 DataRow。
ds.Tables[0].Rows.InsertAt(new DataRow("Seleccione"]),0);