Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this sample table, and working in C# Form Creator Microsoft Visual Studio 2017:

This is my code to add all the rows to the datagridview.

-- My question is:
  1. How to show data that has the same ID, for example, based on the table above, only show rows that have ID 111 and then when the user clicks a button next, it will go to the next ID which is 222?

    What I have tried:

    C#
    string filename = theDialog.FileName;
    StreamReader reader = File.OpenText(filename);
    System.IO.StreamReader file = new System.IO.StreamReader(filename);
    string[] columnnames = file.ReadLine().Split('|');
    DataTable dt = new DataTable();
    foreach (string c in columnnames)
    {
        dt.Columns.Add(c);
    }
    string newline;
    while ((newline = file.ReadLine()) != null)
    {
        DataRow dr = dt.NewRow();
        string[] values = newline.Split('|');
        for (int i = 0; i < values.Length; i++)
        {
            dr[i] = values[i];
        }
        dt.Rows.Add(dr);
    }
Posted
Updated 29-Aug-23 0:45am
v4

Use the Use a BindingSource as the DataSource for your DGV, and use your DataTable to create a DatView which you can use as the DataSource for the BindingSource.

Then use the BindingSource.RowFilter property to determine which rows to display: BindingSource.Filter Property (System.Windows.Forms) | Microsoft Learn[^] includes an example.
 
Share this answer
 
You will need to implement a way to filter your data based on the selected ID and navigate through different ID's when your user clicks the 'NextButton' button -
C#
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace DataGridViewFilter_Sb9m
{
    public partial class MainForm : Form
    {
        private DataTable fullDataTable; //Store all of the loaded data...
        private DataView filteredDataView; //View to display data your user filtered by...
        private int currentRowIndex = 0; //Index of the currently displayed row...

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //Load data from file and create your DataTable...
            string filename = theDialog.FileName;
            using (StreamReader reader = File.OpenText(filename))
            {
                string[] columnnames = reader.ReadLine().Split('|');
                fullDataTable = new DataTable();
                foreach (string c in columnnames)
                {
                    fullDataTable.Columns.Add(c);
                }

                string newline;
                while ((newline = reader.ReadLine()) != null)
                {
                    DataRow dr = fullDataTable.NewRow();
                    string[] values = newline.Split('|');
                    for (int i = 0; i < values.Length; i++)
                    {
                        dr[i] = values[i];
                    }
                    fullDataTable.Rows.Add(dr);
                }
            }

            //Set up the initial DataView with all data...
            filteredDataView = new DataView(fullDataTable);

            //Show data based on the initial index...
            ShowDataAtIndex(currentRowIndex);
        }

        private void ShowDataAtIndex(int index)
        {
            if (index >= 0 && index < filteredDataView.Count)
            {
                DataRowView rowView = filteredDataView[index];
                //Assuming your ID column is named ID as per your supplied code...
                string selectedID = rowView["ID"].ToString();
                //Display the selected ID's data in your DataGridView...
                dataGridView.DataSource = filteredDataView.ToTable();

                //Update label to show the filtered current ID...
                currentIDLabel.Text = "Current ID: " + selectedID;
            }
        }
        //I added this button to load the next rows...
        private void NextButton_Click(object sender, EventArgs e)
        {
            currentRowIndex++;
            if (currentRowIndex >= filteredDataView.Count)
            {
                //If it reached the end, loop back to the beginning...
                currentRowIndex = 0;
            }
            ShowDataAtIndex(currentRowIndex);
        }

        private void FilterButton_Click(object sender, EventArgs e)
        {
            //Filter your data based on the selected ID...
            string selectedID = filterTextBox.Text;
            filteredDataView.RowFilter = $"ID = '{selectedID}'";

            //Reset the current index and display data...
            currentRowIndex = 0;
            ShowDataAtIndex(currentRowIndex);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900