Hi All

I am having a problem on finding a solution regarding the ff problem

I need to do a search using a textbox and a datagrid, that means type in a text on the textbox and press enter and If a record is found the datagrid should point to the found record. I want to emphasize that a record has been found in the datagrid by highlighting the entire row

Can anyone help me with the code or how can I achieve that

Thanks in advance

Recommended Answers

All 3 Replies

Here is a crude example I built from that link I gave:

public partial class Form_DataGrid : Form
    {
        DataTable dt;
        DataView dv;
        CurrencyManager CM;

        public Form_DataGrid()
        {
            InitializeComponent();
        }

        private void Form_DataGrid_Load(object sender, EventArgs e)
        {
            dt = BuildDynamicTableArrayExample();
            dv = new DataView(dt);
            dataGrid1.DataSource = dv;

            dv.Sort = "FieldName1"; // sort field required for search (Find)...

            // Initialize CurrencyManager to hold an instance of the form's CurrencyManager.
            // Needed to manipulate the row pointer...
            CM = (CurrencyManager)dataGrid1.BindingContext[dv];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int row = dv.Find(textBox1.Text); // find the text...

            if (row >= 0)
            {
                CM.Position = row; // position the row pointer...
                dataGrid1.Select(row); // if wanting to highlight the row too...
            }
        }
        
        public static DataTable BuildDynamicTableArrayExample()
        {
            // Create new DataTable object...
            DataTable dt = new DataTable();
            dt.TableName = "MyDataTable";   // Optional unless serializing datatables

            // Add some columns to the table...
            dt.Columns.Add(new DataColumn("FieldName1", typeof(string)));
            dt.Columns.Add(new DataColumn("FieldName2", typeof(int)));   
            dt.Columns.Add(new DataColumn("FieldName3", typeof(double)));

            // Optional primary key field specifier...
            dt.Columns["FieldName1"].Unique = true;
            
            // generate some rows of data...
            for (int r = 0; r < 10; r++)
            {
                // create a row object of table object's type defined above
                DataRow dr = dt.NewRow();   

                // Set each column value in the row...
                dr["FieldName1"] = "data " + r;
                dr["FieldName2"] = r;
                dr["FieldName3"] = (double)r;

                // add the row to the table
                dt.Rows.Add(dr); 
            }
            return dt;
        }

    }

EDIT--NOTE: I do not UnSelect(int rowID) any rows and although it will move the row pointer every time, it will also leave the previous row highlighted.

Thanks man that should solve my problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.