Search Record

  • Thread starter Thread starter cpluspro
  • Start date Start date
C

cpluspro

Guest
I want to find a record from a table. But after finding the reord I cannot set the focus on the record found.

I inserted a datagridview and textboxes from the datasources tab. All I want is to focus the selection on the

record I found both in datagridview and textboxes.

My code is as follows:

I do the search in button2_click

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;



using System.Data.SqlClient;


namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{

// SqlConnection bağlantı = new SqlConnection("Data Source=localhost\\SQLEXPRESS; Initial Catalog=okul; Integrated Security=true");
// SqlConnection bağlantı = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Program Files\\Microsoft SQL Server\\MSSQL10_50.SQLEXPRESS\\MSSQL\\DATA\\okul.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
DataSet ds = new DataSet();
// DataTable türündeki dtable nesnesi oluşturulur:
DataTable dtable = new DataTable();
string resimAdresi; /* OpenFileDialog kontrolünden seçtigimiz dosyanin tam adresini tutacak genel bir degisken. */
int sıra = 0;
int toplamkayit;

public Form1()
{
InitializeComponent();
}

private void kişilerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.kişilerBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.okulDataSet);
}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the okulDataSet.kişiler table. You can move, or remove it, as needed.
this.kişilerTableAdapter.Fill(this.okulDataSet.kişiler);
}

private void button1_Click(object sender, EventArgs e)
{
/* Kullanici bu butona tikladiginda, OpenFileDialog kontrolümüz, dosya açma iletisim kutusunu açar. Kullanici bir dosya seçip OK tusunda bastiginda, Picture Box kontrolümüze seçilen resim dosyasi alinarak gösterilmesi sağlanır. Daha sonra seçilen dosyanin tam adresi label kontrolümüze alınır ve resimAdresi degiskenimize atanır. */
// resim ara
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = System.Drawing.Image.FromFile(openFileDialog1.FileName); /* Drawing isim uzayinda yer alan Image sinifinin FromFile metodunu kullanarak belirtilen adresteki dosya PictureBox kontrolü içine çizilir. */
resimTextBox.Text = openFileDialog1.FileName.ToString();
}

}

private void resimTextBox_TextChanged(object sender, EventArgs e)
{
string resfile = resimTextBox.Text;
resfile = resfile.Trim();
if (resfile != "")
{
pictureBox1.Image = System.Drawing.Image.FromFile(resfile); /* Drawing isim uzayinda yer alan Image sinifinin FromFile metodunu kullanarak belirtilen adresteki dosya PictureBox kontrolü içine çizilir. */
}
else
{
pictureBox1.Image = null;
}
}


//this is where I do the search

private void button2_Click(object sender, EventArgs e)
{
string searched = txtAra.Text;
int sonuc = 0;
//bağlantı.Open();
//this.kişilerTableAdapter = new SqlDataAdapter("SELECT * FROM kişiler", bağlantı);
this.kişilerTableAdapter.Adapter.Fill(this.okulDataSet.kişiler);
//SqlCommandBuilder cb = new SqlCommandBuilder(this.kişilerTableAdapter);
DataRow[] aramasonucu;
aramasonucu = this.okulDataSet.Tables["kişiler"].Select("kişi_no=" + searched + "");
sonuc = aramasonucu.Length;

if (sonuc > 0)
{

DataRow dr;
dr = aramasonucu[0];
kişi_noTextBox.Text = dr[0].ToString();
/* txtad.Text = dr[1].ToString();
txtsoyad.Text = dr[2].ToString();
txtgrup.Text = dr[3].ToString();
txttcno.Text = dr[4].ToString();
dateTimePicker1.Text = dr[5].ToString();
dateTimePicker2.Text = dr[6].ToString();
txtbölüm.Text = dr[7].ToString();
*/
}
else
{
MessageBox.Show("Cannot find record");
}


}

}

}

Continue reading...
 
Back
Top