0
votes

I am trying to change the datagridview row selection base on the value inputted in textbox with suggestappend. My autocompletestringcollection was fetched on my datagrid column reference.

Code for autocompletestringcollection

AutoCompleteStringCollection referenceCollection = new AutoCompleteStringCollection();

int indexOfYourColumn = 1;

                IList list = dgvScriptures.Rows;
                referenceCollection.Clear();

                for (int i = 0; i < list.Count; i++)
                {
                    DataGridViewRow row = (DataGridViewRow)list[i];
                    data = row.Cells[indexOfYourColumn].Value.ToString();
                    referenceCollection.Add(data.ToString());
                }

When type in textbox, value is appended but the datagridview selection was not changed

private void txtSearchReference_TextChanged(object sender, EventArgs e)
        {
            try
            {
                txtSearchReference.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txtSearchReference.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtSearchReference.AutoCompleteCustomSource = referenceCollection;

                if (txtSearchReference.Text != string.Empty)
                {
                    int rowIndex = -1;
                    DataGridViewRow row = dgvScriptures.Rows
                        .Cast<DataGridViewRow>()
                        .Where(r => r.Cells[1].Value.ToString().Contains(txtSearchReference.Text))
                        .FirstOrDefault();
                    rowIndex = row.Index;

                    dgvScriptures.Rows[rowIndex].Selected = true;
                }
            }
            catch (Exception autocomplete)
            {

                MessageBox.Show(autocomplete.ToString());
            }
            
        }

UI Design

1
Does this answer your question? How do I make the DataGridView show the selected row?JohnG
I will try to read it. ThanksDavid King

1 Answers

0
votes

I'd say you'll need to change

dgvScriptures.Rows[rowIndex].Selected = true;

To

dgvScriptures.CurrentCell = dgvScriptures[1,rowIndex];

The 1 being the column you searched/built your autocomplete list from