I have a dataGridView that is populated by a query binding to DataSource. The DGV has a first column as a checkboxcolumn so the user can choose which products he wants to order.
I have also created a "search" functionality (a simple textbox and a button). When the user clicks the button, in the click_event, I redefine the query (something like "...where Name like '%searchword%'"). The search works fine and it filters correctly. The problem is that the checkbox selection loses its value when I re-bind the DGV.
I want to know how I can keep track of the checkbox values.
I tried to put the id's on a list, but I don't know how to handles this if, for example, the user unselects the box (how would I delete it from the list).
Here's the code:
public Main()
{
InitializeComponent();
SendMessage(textBoxSearch.Handle, 0x1501, 1, "Search Product by Name...");
try
{
SQLiteConnection con = new SQLiteConnection(conString);
string prod = "select id,Name,Bulk_Density,Max_Size,Min_Size,WP,GS from Product order by Name";
SQLiteDataAdapter da = new SQLiteDataAdapter(prod, con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "PROD");
dataGridViewProduct.DataSource = ds.Tables["PROD"];
dataGridViewProduct.Columns[1].Visible= false;
dataGridViewProduct.Columns[2].HeaderText = "Name";
dataGridViewProduct.Columns[3].HeaderText = "Bulk Density [g/l]";
dataGridViewProduct.Columns[4].HeaderText = "Max Size [mm]";
dataGridViewProduct.Columns[5].HeaderText = "Min Size [mm]";
dataGridViewProduct.Columns[2].ReadOnly = true;
dataGridViewProduct.Columns[3].ReadOnly = true;
dataGridViewProduct.Columns[4].ReadOnly = true;
dataGridViewProduct.Columns[5].ReadOnly = true;
dataGridViewProduct.Columns[6].ReadOnly = true;
dataGridViewProduct.Columns[7].ReadOnly = true;
dataGridViewProduct.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridViewProduct.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridViewProduct.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridViewProduct.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridViewProduct.Columns[6].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridViewProduct.Columns[7].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridViewProduct.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
con.Close();
}
catch (SQLiteException sqle)
{
MessageBox.Show(sqle.Message);
}
}
private void buttonSearch_Click(object sender, EventArgs e)
{
SQLiteConnection con = new SQLiteConnection(conString);
string search = textBoxSearch.Text;
string prod = "select id,Name,Bulk_Density,Max_Size,Min_Size,WP,GS from Product where Name like '%"+search+"%' order by Name";
SQLiteDataAdapter da = new SQLiteDataAdapter(prod, con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "PROD");
dataGridViewProduct.DataSource = ds.Tables["PROD"];
}
Aucun commentaire:
Enregistrer un commentaire