简体   繁体   English

根据文本框输入值更改 C# Datagridview 行选择

[英]Change C# Datagridview row selection base on textbox input value

I am trying to change the datagridview row selection base on the value inputted in textbox with suggestappend.我正在尝试根据在带有Suggestappend的文本框中输入的值来更改datagridview行选择。 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在文本框中键入时,附加值但未更改 datagridview 选择

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用户界面设计

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 1 是您搜索/构建自动完成列表的列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM