Resolved How to read text with datagridview1 - column1 and display filtered rows with datagridview2 ?

Paraman

New member
Joined
Oct 19, 2024
Messages
3
Programming Experience
10+
Hi,
I am using win-forms applications & c#. I wish to read the text from datagridview1-column1, and I wish to filter the table rows according to the related column1- text values & need to display it in datagridview2.
Currently Iam using it from DataGridView to Listview. But because of Image columns I am in need to display it from datagridview1 to datagridview2. Any kind help will be more useful. Thanks!

C#:
    private void MyDataGrid1_EditingControlShowing(System.Object Sender, DataGridViewEditingControlShowingEventArgs e) {
        MyDGVCurRow = myDataGrid1.CurrentCell.RowIndex;
        if ((TextBox) (e.Control)) {
            ((TextBox)(e.Control)).CharacterCasing = CharacterCasing.Upper;
        }
        e.Control.TextChanged -= new EventHandler(this.Form_Test.MyDGVColumn_LstVew);
        if (myDataGrid1.CurrentCell.ColumnIndex == 1) {
            myDataGrid1.CurrentCell = myDataGrid1[1, MyDGVCurRow];
            e.Control.TextChanged += new EventHandler(this.Form_Test.MyDGVColumn_LstVew);
        }
    }

    private void MyDGVColumn_LstVew(System.Object Sender, EventArgs e) {
            if (myDataGrid1.CurrentCell.ColumnIndex == 1) {
                TextBox thisBox =(TextBox)(Sender);

                //Data Collection
                MyItemCollect();

                MyDGVDataSource = MyDataTabl1;

                //DataView MyItemFiltView = MyDataTable1.DefaultView;
                //MyItemFiltView.RowFilter = null;
                //MyItemFiltView.RowFilter = "item_type='" + thisBox.Text + "'";

                //DataTable MyDataTable2 = MyItemFiltView.ToTable(); ??????????? Images Disappearing while using filter
                //for (int N1 = 0; N1 <= MyDataTbl.Rows.Count - 1; N1++) {
                //ChkItm = String.Empty;
                //ChkItm = MyDataTbl.Rows[N1]["item_type"].ToString().Trim().ToUpper();
                //Naomi = ChkItm.StartsWith(thisBox.Text);
                //if (Naomi == true) {
                //DataRow MyDRw = MyDGVItemDtb.NewRow();
                //MyDRw["item_type"] = MyDataTbl.Rows[N1]["item_type"].ToString();
                //MyDRw["item_description"] = MyDataTbl.Rows[N1]["item_description"].ToString();
                //MyDRw["item_code"] = MyDataTbl.Rows[N1]["item_code"].ToString();
                //MyDRw["hsn_code"] = MyDataTbl.Rows[N1]["hsn_code"].ToString();
                //MyDRw["item_image"] = MyDataTbl.Rows[N1]["item_image"];
                //MyDRw["item_units"] = MyDataTbl.Rows[N1]["item_units"].ToString();
                //MyDRw["item_rate"] = MyDataTbl.Rows[N1]["item_rate"].ToString();
                //MyDGVItemDtb.Rows.Add(MyDRw);


                .MyDataGrid2.Visible = false;
                if (.MyDataGrid2.Rows.Count != 0) {
                    .MyDataGrid2.ClearSelection();
                    .MyDataGrid2.BeginEdit(false);
                    .MyDataGrid2.Visible = true;
                }
         
                //myDataGrid1.BeginEdit(true);
                //myDataGrid1.Rows[0].Cells[1].Selected = true;

                myDataGrid1.CurrentCell = myDataGrid1.Rows[0].Cells[1];
                myDataGrid1.Rows[0].Selected = true;
                myDataGrid1.Rows[0].Cells[1].Selected = true;
                myDataGrid1.Select();
                myDataGrid1.Focus();

                //myDataGrid1.CurrentCell.Selected = true;
                //myDataGrid1.BeginEdit(true);


            }
    }
The above codes while I press any key simply moving to next cell with datagrid1 and not continue to read the text in same cell until pressing <Enter> Key. Thanks for the helps!
 
Solution
Here's an example of how you might build the DataSet:
C#:
    var data = new DataSet();
    var parent As DataTable = GetParentTable();
    var child As DataTable = GetChildTable();

    data.Tables.Add(parent);
    data.Tables.Add(child);

    // Add a relationship between the ID of the parent
    // table and the ParentID of the child table.
    data.Relations.Add("ParentChild",
                       parent.Columns["ID"],
                       child.Columns["ParentID"]);
and here's an example of how you might bind it:
C#:
    // Bind the parent source to the parent table.
    BindingSource1.DataSource = data;
    BindingSource1.DataMember = "Parent";

    // Bind the child source to the relationship.
    BindingSource2.DataSource =...
You have two separate problems:
1) Reading from the first data grid view; and
2) Filtering the data in your second data grid view.

Reading data from your first data grid view (e.g. datagridview1) should be the same regardless of whether you are using what you read for controlling another data grid view or a list view. If you could read previously read from the first data grid view to control your list view, then you would use exactly the same code to read the value to control your new second data grid view.

Once you have the value from the first data grid view, you can use that value to create your filter. To display the filtered view in the second data grid view, you simply assign the results of that filtering to the DataSource of the second data grid view.
 
You probably don't need any code at all. Just add two DataTables to a DataSet with a DataRelation between them. Bind a parent BindingSource between the parent DataTable and the parent DataGridView and a child BindingSource between the DataRelation and the child DataGridView. Selecting a row in the parent grid will then automatically display only the related child rows in the child grid; no code required.
 
Here's an example of how you might build the DataSet:
C#:
    var data = new DataSet();
    var parent As DataTable = GetParentTable();
    var child As DataTable = GetChildTable();

    data.Tables.Add(parent);
    data.Tables.Add(child);

    // Add a relationship between the ID of the parent
    // table and the ParentID of the child table.
    data.Relations.Add("ParentChild",
                       parent.Columns["ID"],
                       child.Columns["ParentID"]);
and here's an example of how you might bind it:
C#:
    // Bind the parent source to the parent table.
    BindingSource1.DataSource = data;
    BindingSource1.DataMember = "Parent";

    // Bind the child source to the relationship.
    BindingSource2.DataSource = BindingSource1;
    BindingSource2.DataMember = "ParentChild";

    // Bind the parent control to the parent source.
    DataGridView1.DataSource = BindingSource1;

    // Bind the child control to the child source.
    DataGridView2.DataSource = BindingSource2;
 
Solution

Latest posts

Back
Top Bottom