I have a query regarding items selected by means of a CheckBox
within DataGridView
. The thing is, I load mi DataGridView
and add CheckBoxes
. What happens is that I have a TextBox
that helps to filter when I press numbers to filter everything goes well but when I delete them CheckBox
they change position and the CheckBox
. Attached a GIF
reference:
As you will see, CheckBox
they change places at the time of filtering and are deselected. Does anyone have an idea how to fix that?
I leave the method with which I filter:
private void filtrarTitulo(string valor)
{
SqlConnection conexion = new SqlConnection();
conexion.ConnectionString = "cadena de conexión";
conexion.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT Titulo FROM V_CuetaWeb WHERE Titulo LIKE ('" + valor + "%') ORDER BY Titulo DESC", conexion);
da.Fill(ds, "Cuotas");
conexion.Close();
dtgTitulo.DataSource = ds;
dtgTitulo.DataMember = "Cuotas";
}
Where I use it:
private void txtTitulo_TextChanged(object sender, EventArgs e)
{
filtrarTitulo(txtTitulo.Text.ToString().Trim());
}
How do I assign the CheckBox
to DataGridView
:
private void Form1_Load(object sender, EventArgs e)
{
llenaTitulo();
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
chk.HeaderText = "Seleccione";
chk.Name = "check";
dtgTitulo.Columns.Add(chk);
dtgTitulo.AllowUserToAddRows = false;
}
And as I charge the DataGridView
:
public void llenaTitulo()
{
try
{
string conn = "cadena de conexión";
using (SqlConnection conexion = new SqlConnection(conn))
{
conexion.Open();
string query = "SELECT Titulo AS Título FROM V_CuetaWeb GROUP BY Titulo ORDER BY Titulo DESC";
SqlCommand sqlCommand = new SqlCommand(query, conexion);
SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
DataSet ds = new DataSet();
da.Fill(ds);
dtgTitulo.DataSource = ds.Tables[0];
conexion.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
problem one: Se cambian de lugar los ChckcBox
problem two: Se deseleccionan los CheckBox
Any suggestion?
UPDATE:
private void filtrarTituloDos(string valor)
{
List<DataGridViewRow> filtro = new List<DataGridViewRow>();
foreach (DataGridViewRow fila in dtgTitulo.Rows)
{
//Asumo que el campo a filtrar se llama Titulo
//Filtramos las filas con campo Titulo igual a valor
if (fila.Cells["Título"].Value.ToString() == valor)
{
filtro.Add(fila);
}
}
//Borrrando todas la filas actuales del DataGridView
dtgTitulo.Rows.Clear();
foreach (DataGridViewRow fila in filtro)
{
//Agregamos las filas filtradas al DataGridView
dtgTitulo.Rows.Add(fila);
}
}
private void txtTitulo_TextChanged(object sender, EventArgs e)
{
lblCaracterTitulo.Text = Convert.ToString(8 - txtTitulo.Text.Length);
filtrarTituloDos(txtTitulo.Text.ToString().Trim());
}
Problem 1: System.ArgumentException: 'No se puede borrar esta lista.'
in the line: dtgTitulo.Rows.Clear();
at the time of typing in the TextBox
to filter
Problem 2: If I comment out the line from the problem above, it happens that it doesn't filter anything. Suggestions?
Do you think if in the filltitle() method you also put the code of the checkbox columns:
I think that would be the solution. slds
Here is an example of what I would do without using databases or other objects:
I put a variant that can be used to solve your problem, note that I create a
DataGridView
, just to show theitems
filtered ones, I assign it the same location and size as thedtgTitulo
, which implies that it will be shown superimposed on theDataGridView dtgTitulo
, but you can give it another location, even you could display it in another form as a popup.The procedure
filtrarTitulo
.In your original code you make the call to the procedure
filtrarTitulo
in the eventTextChanged
of theTextBox
, that is not convenient, because you would be executing the call every time the user presses a key, I recommend adding a button that the user can press when he finishes writing the text and want to apply the filter.Also in your procedure
llenaTitulo
, you must do the following, in addition to everything you have already done.