sa form load po nakalagay yung code na pang countBaka 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
private void frm_student_list_Load(object sender, EventArgs e)
{
RefreshDataGridView(TotalStudents);
txtbTotal.Text = dataGridView1.Rows.Count.ToString();
}
try mo to: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();
dataGridView1.Rows.Count or dataGridView1.RowCount
zero kasi start ng count =D try mo mag add ng another items
Yung code mismo sa pag mo ng items? kung ok lang try mo pakita
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)
{
}
}
}
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 langDi 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); }
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!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) { } } }