modify suggestive behaviour of datagridview
I have a textbox in my form and when the user types inside it , a datagridview (named dataGridView1) which was hidden earlier becomes visible and used to display similar results by using dataview. below is the code
private void Form2_Load(object sender, EventArgs e)
{
dataGridView1.Visible = false;
dataGridView1.ColumnHeadersVisible = false;
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Data Source =DESKTOP-197P3FU\\SQLEXPRESS;Integrated security=SSPI;database=unk";
// Create a data adapter to retrieve data from the table
SqlDataAdapter adapter = new SqlDataAdapter($"SELECT * FROM ledgernames", conn);
// Initialize the dataset
dataSet = new DataSet();
// Fill the dataset with the table data
adapter.Fill(dataSet);
}
dataGridView1.DataSource = dataSet.Tables[0];
}
private void customtextbox3_Enter(object sender, EventArgs e)
{
dataGridView1.Visible = true;
// Hide unwanted columns
dataGridView1.Columns["id"].Visible = false;
dataGridView1.Columns["current_balance"].Visible = false;
dataGridView1.Columns["opening_balance"].Visible = false;
//
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[1];
}
private void customtextbox3_TextChanged(object sender, EventArgs e)
{
string searchText = customtextbox3.Text.Trim();
dataView = dataSet.Tables[0].DefaultView;
if (searchText.Length > 0) //
{
dataView.RowFilter = $"name LIKE '%{searchText}%'";
if (dataView.Count == 0) // check if no rows match the filter
{
searchText = searchText.Substring(0, searchText.Length - 1);
customtextbox3.Text = searchText;
}
else
{
dataGridView1.DataSource = dataView;
}
}
else
{
dataGridView1.DataSource = dataSet.Tables[0];
}
customtextbox3.Select(customtextbox3.Text.Length, 0); // Set cursor at the end
}
private void customtextbox3_KeyDown(object sender, KeyEventArgs e)
{
if (dataGridView1.CurrentCell == null)
{
return;
}
else
{
rpos = dataGridView1.CurrentCell.RowIndex;
cpos = dataGridView1.CurrentCell.ColumnIndex;
switch (e.KeyCode)
{
case Keys.Up:
rpos--;
if (rpos >= 0) dataGridView1.CurrentCell = dataGridView1.Rows[rpos].Cells[cpos];
e.Handled = true;
break;
case Keys.Down:
rpos++;
if (rpos < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[rpos].Cells[cpos];
e.Handled = true;
break;
case Keys.Enter:
{
if (dataGridView1.Rows.Count > 0)
{
dataGridView1.Visible = false;
customtextbox3.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
customControl11.Focus();
dataView.RowFilter = ""; // resets the filter to its starting state
}
e.Handled = e.SuppressKeyPress = true;
break;
}
}
}
}
what i am trying to get is when a perfect similar word like if user types ris in textbox , the suggestions were (by datagridview1) would be prisha , enterprises , rishabh , rishi . but the last two should be in bold and blue color (as they start with ris ) and when the user by using
customtextbox3_KeyDown
event tries to move up and down , the very first time the movement of up and down should start from rishabh . I have tried many options but none of them work . Please suggest the code for that .