Merhaba arkadaşlar bu makalemizde dataGridView nesnesinde dataView metodunu kullanarak fiyata göre filtreleme işlemini gerçekleştireceğiz.
Şekil 1
Şekil 2
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dataview_filter
{
public partial class Form1 : Form
{
SqlConnection baglan;
SqlDataAdapter da;
DataSet ds;
DataView dv;
string sql = null;
string conn = null;
SqlCommand cmd;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
baglan = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|kategori.mdf;Integrated Security=True;Connect Timeout=30");
sql = "Select * From [Table]";
da = new SqlDataAdapter(sql, baglan);
ds = new DataSet();
da.Fill(ds);
baglan.Open();
dataGridView1.DataSource = ds.Tables[0]; ;
baglan.Close();
}
private void BtnFilter_Click(object sender, EventArgs e)
{
ds = new DataSet();
sql = "Select * From [Table]";
conn= "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|kategori.mdf;Integrated Security=True;Connect Timeout=30";
baglan = new SqlConnection(conn);
da= new SqlDataAdapter();
try
{
baglan.Open();
cmd = new SqlCommand(sql, baglan);
da.SelectCommand = cmd;
da.Fill(ds);
da.Dispose();
cmd.Dispose();
baglan.Close();
dv = new DataView(ds.Tables[0], "Fiyat < = " + Convert.ToInt32(txtFilter.Text) , "Fiyat", DataViewRowState.CurrentRows);
dataGridView1.DataSource = dv;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
0 comments:
Yorum Gönder