What's new

Help Help how to count datagrindview rows?

Status
Not open for further replies.

shion_

Eternal Poster
Established
Joined
Apr 28, 2016
Posts
1,177
Reaction
381
Points
465
Ayaw po gumana patulong po, hindi binibilang yung lamang ng datagridview



C#:
txtbTotal.Text = dataGridView1.Rows.Count.ToString();
 
Baka nagcacount ka before mag add ng items. Try mo pakita codes mo

Or pag add mo ng item, increment mo
like this:
int count = dataGridView1.Rows.Count + 1;
txtbTotal.Text = count.ToString();

or

txtbTotal.Text = dataGridView1.RowCount.ToString();
 
Last edited:
Baka nagcacount ka before mag add ng items. Try mo pakita codes mo

Or pag add mo ng item, increment mo
like this:
int count = dataGridView1.Rows.Count + 1;
txtbTotal.Text = count.ToString();
sa form load po nakalagay yung code na pang count
 
Ahm di ko kasi alam codes mo.
Baka nauuna yung count before mag load items sa form load
C#:
private void frm_student_list_Load(object sender, EventArgs e)
        {
            RefreshDataGridView(TotalStudents);
            txtbTotal.Text = dataGridView1.Rows.Count.ToString();
        }
 
C#:
private void frm_student_list_Load(object sender, EventArgs e)
        {
            RefreshDataGridView(TotalStudents);
            txtbTotal.Text = dataGridView1.Rows.Count.ToString();
        }
try mo to:
txtbTotal.Text = dataGridView1.RowCount.ToString();
 
try mo to:
txtbTotal.Text = dataGridView1.RowCount.ToString();
Untisadadasdadtled.png

ayaw po talaga
 

Attachments

Yung code mismo sa pag mo ng items? kung ok lang try mo pakita
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CsharpCollection
{
    public partial class frm_student_list : Form
    {

        public List<Student> _Students { get; set; } = new List<Student>();

        public frm_student_list()
        {
            InitializeComponent();
            RefreshDataGridView(_Students);
        
        }
        
         private void RefreshDataGridView(List<Student> students)
        {
            dataGridView1.Rows.Clear();
            foreach (var student in students)
            {
                dataGridView1.Rows.Add(
                    student.Lastname,
                    student.Firstname,
                    student.Course,
                    student.YearLevel,
                    student.Section);
            }
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            frm_student_entry frm = new frm_student_entry();
            frm.Students = _Students;
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                _Students.Add(frm.stud);
                RefreshDataGridView(_Students);
            }
        }

        private void frm_student_list_Load(object sender, EventArgs e)
        {
            txtbTotal.Text = dataGridView1.RowCount.ToString();
            RefreshDataGridView(_Students);
            
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            List<Student> Results = new List<Student>();

            foreach (var student in _Students)
            {
                string studentname = string.Concat(student.Lastname, "", student.Firstname).ToLower();
                if (studentname.Contains(txtSearch.Text.ToLower()))

                    Results.Add(student);

                {
                    RefreshDataGridView(Results);
                }
            }
        }

        private void txtbTotal_TextChanged(object sender, EventArgs e)
        {
            

        }
    }
}
 
Di talaga mag cacount. Dahil nag aad ka ng Students item sa
private void txtSearch_TextChanged(object sender, EventArgs e)
Which is di nagfafuntion sa form load mo.

So ilagay mo to
txtbTotal.Text = dataGridView1.RowCount.ToString();

after mo mag type sa textbox sa
void txtSearch_TextChanged(object sender, EventArgs e)

And this is wrong also. Nauuna ang count bago mag add/refresh

C#:
private void frm_student_list_Load(object sender, EventArgs e)
{
    txtbTotal.Text = dataGridView1.RowCount.ToString();
    RefreshDataGridView(_Students);

}
 
Last edited:
Di talaga mag cacount. Dahil nag aad ka ng Students item sa
private void txtSearch_TextChanged(object sender, EventArgs e)
Which is di nagfafuntion sa form load mo.

So ilagay mo to
txtbTotal.Text = dataGridView1.RowCount.ToString();

after mo mag type sa textbox sa
void txtSearch_TextChanged(object sender, EventArgs e)

And this is wrong also. Nauuna ang count bago mag add/refresh

C#:
private void frm_student_list_Load(object sender, EventArgs e)
{
    txtbTotal.Text = dataGridView1.RowCount.ToString();
    RefreshDataGridView(_Students);

}
hindi pa rin po nagcount pag nasa form load naman may zero lang
 
Take this as an example :)
So magcacount din to after magsearch sa textbox

C#:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace testasd
{  
    public record Students(string Lastname, string Firstname, string Course);

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public List<Students> _Students { get; set; } = new List<Students>();

        private void Form1_Load(object sender, EventArgs e)
        {
            List<Students> infos = new List<Students>();

            infos.Add(new Students("juan", "qwe", "it"));
            infos.Add(new Students("john", "dd", "it"));
            infos.Add(new Students("pedro", "dasdd", "it"));
            _Students.AddRange(infos);

            RefreshDataGridView(_Students);
           
        }

        void CountItems()
        {
            this.Text = dataGridView1.RowCount.ToString();
        }

        private void RefreshDataGridView(List<Students> students)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.ColumnCount = 3;

            dataGridView1.Columns[0].Name = "Last Name";
            dataGridView1.Columns[1].Name = "First Name";
            dataGridView1.Columns[2].Name = "Course";
            foreach (var student in students)
            {
                dataGridView1.Rows.Add(
                    student.Lastname,
                    student.Firstname,
                    student.Course);
            }

            CountItems();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            List<Students> Results = new List<Students>();

            foreach (var student in _Students)
            {
                string studentname = string.Concat(student.Lastname, "", student.Firstname).ToLower();
                if (studentname.Contains(textBox1.Text.ToLower()))

                    Results.Add(student);

                {
                    RefreshDataGridView(Results);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
           
        }
    }
}
 
Last edited:
Take this as an example :)
So magcacount din to after magsearch sa textbox

C#:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace testasd
{ 
    public record Students(string Lastname, string Firstname, string Course);

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public List<Students> _Students { get; set; } = new List<Students>();

        private void Form1_Load(object sender, EventArgs e)
        {
            List<Students> infos = new List<Students>();

            infos.Add(new Students("juan", "qwe", "it"));
            infos.Add(new Students("john", "dd", "it"));
            infos.Add(new Students("pedro", "dasdd", "it"));
            _Students.AddRange(infos);

            RefreshDataGridView(_Students);
          
        }

        void CountItems()
        {
            this.Text = dataGridView1.RowCount.ToString();
        }

        private void RefreshDataGridView(List<Students> students)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.ColumnCount = 3;

            dataGridView1.Columns[0].Name = "Last Name";
            dataGridView1.Columns[1].Name = "First Name";
            dataGridView1.Columns[2].Name = "Course";
            foreach (var student in students)
            {
                dataGridView1.Rows.Add(
                    student.Lastname,
                    student.Firstname,
                    student.Course);
            }

            CountItems();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            List<Students> Results = new List<Students>();

            foreach (var student in _Students)
            {
                string studentname = string.Concat(student.Lastname, "", student.Firstname).ToLower();
                if (studentname.Contains(textBox1.Text.ToLower()))

                    Results.Add(student);

                {
                    RefreshDataGridView(Results);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
          
          
        }
    }
}
Thank you po! pangalawang beses!
 
Status
Not open for further replies.

Similar threads

Back
Top