Question

I'm using a datagrid to get a view on my app.

I need to be able to select and show the a row depending on the input of a textbox(txtpallet).

The datagrid has a scroll bar. I'm able to highlight the row I want but the user still need to scroll down to it.

I want the datagrid to automatically scroll down to the row i selected.

I'm not sure how to do this. I have struggled too long now and would like some help please.

I've made a small test app. please see code below.

I'm using c# .net 2.0 compact framework in Device application (Windows CE)

private void populateGrid()
    {
        try
        {
            string sub = "my connection string";

            OracleConnection conn = new OracleConnection(sub);
            string query;
            query = "Select emp_id_no,emp_username from dc_emp order by emp_id_no";
            {
                conn.Open();

                using (OracleDataAdapter a = new OracleDataAdapter(query, conn))
                {
                    DataTable t = new DataTable();
                    a.Fill(t);

                    dgSku.TableStyles.Clear();
                    DataGridTableStyle tableStyle = new DataGridTableStyle();
                    tableStyle.MappingName = t.TableName;

                    foreach (DataColumn item in t.Columns)
                    {
                        DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
                        tbcName.Width = 80;
                        tbcName.MappingName = item.ColumnName;
                        tbcName.HeaderText = item.ColumnName;
                        tableStyle.GridColumnStyles.Add(tbcName);
                    }
                    dgSku.TableStyles.Add(tableStyle);

                    dgSku.DataSource = t;

                    DataRow[] datarow = t.Select("emp_username like '" + txtpallet.Text + "' ");

                    foreach (DataRow dr in datarow)
                    {
                        MessageBox.Show("Index is " + dr.Table.Rows.IndexOf(dr));
                        dgSku.Select(dr.Table.Rows.IndexOf(dr));
                    }
                }
            }
            conn.Close();
        }
        catch (OracleException x)
        {
            MessageBox.Show("" + x);
        }
    }

Here is two examples: A) selecting something at the top of the list B) selecting something at the bottom somewhere

A)

enter image description here

B)

enter image description here

Was it helpful?

Solution

I got it. You need to get the Row index, which i do in the code at the top. Then simply use:

dgSku.CurrentRowIndex = dr.Table.Rows.IndexOf(dr);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top