c# DataRepeater Control in winform (windows formlarında repeater kullanımı)
Asp.net de kullandığımız repeater ile yapabildiğimiz aşağıdaki kayıt listeleme işlemini windows formda yapabilir miyiz?
DataRepeater Control kullanacağız.Aşğıda makale mevcut
DataRepeater Control in C#.Net
The DataRepeater control allows you to display rows of your data in a scrollable
container giving you more flexibility and customization than standard grid
controls.
How to use DataRepeater
Drag and drop DataRepeater control from toolbox on the window form.
Drag and drop textbox in the DataRepeater Control to display data.
BindData in DataRepeater
Code:
using
System;
using
System.Data;
using
System.Data.SqlClient;
using
System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsApplication1
{
public partial
class
frmDataRepeater : Form
{
public frmDataRepeater()
{
InitializeComponent();
}
private void
frmDataRepeater_Load(object sender,
EventArgs e)
{
//Connect to the Database
string constring =
"Server=(local);Database=my; User Id=sa; Password=sa";
SqlConnection sqlcon =
new SqlConnection(constring);
//Open the connection
sqlcon.Open();
SqlCommand cmd =
new SqlCommand();
SqlDataAdapter da =
new SqlDataAdapter();
// Write the query
cmd.CommandText = "select * from regform";
da.SelectCommand = cmd;
cmd.Connection = sqlcon;
da.SelectCommand = cmd;
//create a dataset
DataSet ds =
new DataSet();
da.Fill(ds);
//Close the connection
sqlcon.Close();
// Bind data to TextBox
textBox1.DataBindings.Add("Text", da,
"id");
textBox2.DataBindings.Add("Text", da,
"pass");
textBox3.DataBindings.Add("Text", da,
"name");
dataRepeater1.Visible = true;
dataRepeater1.DataSource = ds.Tables[0];
}
}
}
access kullananlar için
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xxxx.accdb";
OleDbConnection connection = new OleDbConnection(connectionstring);
connection.Open();
DataSet ds = new DataSet();
using (OleDbCommand command = new OleDbCommand("select * from vadeli_borclar", connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
adapter.Fill(ds);
}
// Bind data to TextBox
label4.DataBindings.Add("Text", ds.Tables[0], "adsoyad");
label7.DataBindings.Add("Text", ds.Tables[0], "tutar");
label5.DataBindings.Add("Text", ds.Tables[0], "tarih");
//tek bir veri almak için
for(int a=0;a<ds.Tables[0].Rows.Count;a++)
{
MessageBox.Show(ds.Tables[0].Rows[a]["alis_fiyati"].ToString() + "--" + ds.Tables[0].Rows[a]["odedigi"].ToString());;
}
dataRepeater1.Visible = true;
dataRepeater1.DataSource = ds.Tables[0];
Data will bind to the DataRepeater when application run.
Run the project
You can see next data by making scroll bar down.
Bir kaç özellik daha eğer sol tarafta çıkan mavi ok çıkmasın isterseniz.
repeater a eklediğimiz label7 nin içindeki değeri almak içinse
private void dataRepeater1_CurrentItemIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(dataRepeater1.CurrentItem.Controls["label7"].Text);
}
Hiç yorum yok :
Yorum Gönder