1

Datagrid automatically moves down to select C #

 3 years ago
source link: https://www.codesd.com/item/datagrid-automatically-moves-down-to-select-c.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Datagrid automatically moves down to select C #

advertisements

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

BiBs9.png
QLA5b.png

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);


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK