Merhaba arkadaşlar bu makalemizde dataGridView nesnesinde boş satır var mı kontrolünü yapacağız. Bulunan boş satırları mesaj olarak ekranda gösterimini sağlayacağız.
Şekil 1
Şekil 2
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace datagridview_check_rows_empty
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.bindData();
}
private void btnCheck_Click(object sender, EventArgs e)
{
List<int> listRow = new List<int>();
bool rowEmpty = true;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (!string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[j].Value.ToString()))
{
rowEmpty = false;
break;
}
else
{
rowEmpty = true;
}
}
if (rowEmpty)
{
listRow.Add(i + 1);
}
}
string msg = string.Join(",", listRow.ToArray());
MessageBox.Show("Row " + msg + " is empty." + Environment.NewLine + msg + " satir(lari) bos.");
}
private void bindData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[7] { new DataColumn("Id", typeof(string)),
new DataColumn("FirstName", typeof(string)),
new DataColumn("LastName",typeof(string)),
new DataColumn("Contact",typeof(string)),
new DataColumn("Mail",typeof(string)),
new DataColumn("City",typeof(string)),
new DataColumn("Country",typeof(string))});
dt.Rows.Add("1", "Bahadir", "Sahin", "+1 (212) 221-4937","basahin@hotmail.com","New York","USA");
dt.Rows.Add("2", "Melissa", "Parker", "+1 (425) 882-8080", "melparker@gmail.com", "Redmond", "USA");
dt.Rows.Add("", "", "", "", "", "", "");
dt.Rows.Add("4", "Tommy", "Paton", "+1 (867) 920-2233", "tommypat@hotmail.com", "Yellowknife", "Canada");
dt.Rows.Add("5", "Alice", "May", "+1 (408) 996-1010", "alicemay12@hotmail.com", "Cupertino", "USA");
dt.Rows.Add("6", "Michael", "Alon", "+1 (650) 644-3358", "michaelal57@gmail.com", "Mountain View", "USA");
dt.Rows.Add("", "", "", "", "", "", "");
dt.Rows.Add("8", "Mike", "Carson", "+1 (775) 223-7665", "mikecarson33@hotmail.com", "Reno", "USA");
dt.Rows.Add("", "", "", "", "", "", "");
dt.Rows.Add("10", "Tracy", "Abbot", "+1 (407) 999-7788", "tracyabbot82@hotmail.com", "Orlando", "USA");
dataGridView1.DataSource = dt;
}
}
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder