Imam neku bazu i zelio bih da kad misem kliknem na ime,prezime grad ili sta vec u tablici(nekog zaposlenika,ijec je o bazi northwind,table employees) da mi se to binda na textboxove tj da mi se prikazu podaci od to klijenta u textboxovima poput
firstname,lastname,city...itd, znaci isto sto i metode movelast,movefirst,movenext i moveprevious rade,samo sto bi ovo bilo bolje.
Evo citav kod: ALi nema potrebe sve da pregledavate, nego samo "private void dg_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)"
Code:
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 My_WindowsForm_Example
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("server=PC;integrated security=true;database=northwind");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
BindingSource tblNamesBS = new BindingSource();
public Form1()
{
InitializeComponent();
}
private void btnAddRecord_Click(object sender, EventArgs e)
{
da.InsertCommand = new SqlCommand("insert into employees (LastName,FirstName,Title,Country,City,BirthDate) values(@LastName,@FirstName,@Title,@COuntry,@City,@BirthDate)", conn);
da.InsertCommand.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = txtLastName.Text;
da.InsertCommand.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = txtFirstName.Text;
da.InsertCommand.Parameters.Add("@Title", SqlDbType.NVarChar).Value = txtTitle.Text;
da.InsertCommand.Parameters.Add("@Country", SqlDbType.NVarChar).Value = txtCountry .Text ;
da.InsertCommand.Parameters.Add("@City", SqlDbType.NVarChar).Value = txtCity .Text ;
da.InsertCommand.Parameters.Add("@BirthDate", SqlDbType.DateTime).Value =Convert .ToDateTime ( txtBirthDate.Text);
conn.Open();
da.InsertCommand.ExecuteNonQuery();
conn.Close();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
da.SelectCommand = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country,City,BirthDate from Employees",conn );
ds.Clear();
da.Fill(ds,"Employees");
dg.DataSource = ds.Tables["Employees"];
tblNamesBS .DataSource =ds.Tables ["Employees"];
txtFirstName.DataBindings.Clear();
txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
txtLastName.DataBindings.Clear();
txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "LastName"));
txtTitle.DataBindings.Clear();
txtTitle.DataBindings.Add(new Binding("Text", tblNamesBS, "Title"));
txtCountry.DataBindings.Clear();
txtCountry.DataBindings.Add(new Binding("Text", tblNamesBS, "Country"));
txtCity.DataBindings.Clear();
txtCity.DataBindings.Add(new Binding("Text", tblNamesBS, "City"));
txtBirthDate.DataBindings.Clear();
txtBirthDate.DataBindings.Add(new Binding("Text", tblNamesBS, "BirthDate"));
Records();
}
private void btnFirst_Click(object sender, EventArgs e)
{
tblNamesBS.MoveFirst();
dgUpdate();
Records();
}
private void btnPrevious_Click(object sender, EventArgs e)
{
tblNamesBS.MovePrevious();
dgUpdate();
Records();
}
private void btnNext_Click(object sender, EventArgs e)
{
tblNamesBS.MoveNext();
dgUpdate();
Records();
}
private void btnLast_Click(object sender, EventArgs e)
{
tblNamesBS.MoveLast();
dgUpdate();
Records();
}
private void dgUpdate()
{
dg.ClearSelection();
dg.Rows[tblNamesBS.Position].Selected = true;
Records();
}
private void Records()
{
label7.Text = "Record " + (tblNamesBS.Position + 1) + " of " + tblNamesBS.Count;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
da.UpdateCommand = new SqlCommand("Update Employees set lastname=@lastname,firstname=@firstname,title=@title,country=@country,city=@city,birthdate=@birthdate where EmployeeID=@ID",conn );
da.UpdateCommand.Parameters.Add("@lastname", SqlDbType.NVarChar).Value = txtLastName.Text;
da.UpdateCommand.Parameters.Add("@firstname", SqlDbType.NVarChar).Value = txtFirstName.Text;
da.UpdateCommand.Parameters.Add("@title", SqlDbType.NVarChar).Value = txtTitle.Text;
da.UpdateCommand.Parameters.Add("@country", SqlDbType.NVarChar).Value = txtCountry.Text;
da.UpdateCommand.Parameters.Add("@city", SqlDbType.NVarChar).Value = txtCity.Text;
da.UpdateCommand.Parameters.Add("@birthdate", SqlDbType.DateTime ).Value = Convert .ToDateTime (txtBirthDate.Text);
da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables["Employees"].Rows[tblNamesBS.Position][0];
conn.Open();
da.UpdateCommand.ExecuteNonQuery();
conn.Close();
}
private void btnDelete_Click(object sender, EventArgs e)
{
DialogResult dr = new DialogResult();
dr=MessageBox.Show("Are You Sure?\nThere is no undo once data is deleted", "Confirm Deletion", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
da.DeleteCommand = new SqlCommand("delete from Employees where EmployeeID=@ID and FirstName=@FirstName", conn);
da.DeleteCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables["Employees"].Rows[tblNamesBS.Position][0];
da.DeleteCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
conn.Open();
da.DeleteCommand.ExecuteNonQuery();
conn.Close();
}
else
{
MessageBox.Show("Deletion canceled");
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
da.SelectCommand = new SqlCommand("select firstname,lastname,title,country,city,birthdate from employees where lastname=@lastname and firstname=@firstname ",conn);
da.SelectCommand.Parameters.Add("@lastname", SqlDbType.NVarChar).Value = txtLastName.Text;
da.SelectCommand.Parameters.Add("@firstname", SqlDbType.NVarChar).Value = txtFirstName.Text;
conn.Open();
da.SelectCommand.ExecuteNonQuery();
ds.Clear();
da.Fill(ds, "Employees");
dg.DataSource = ds.Tables["Employees"];
conn.Close();
}
private void dg_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dg.Rows[tblNamesBS.Position].Selected = true;//Ovo nece radit kako treba
}
}
}
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 My_WindowsForm_Example
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("server=PC;integrated security=true;database=northwind");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
BindingSource tblNamesBS = new BindingSource();
public Form1()
{
InitializeComponent();
}
private void btnAddRecord_Click(object sender, EventArgs e)
{
da.InsertCommand = new SqlCommand("insert into employees (LastName,FirstName,Title,Country,City,BirthDate) values(@LastName,@FirstName,@Title,@COuntry,@City,@BirthDate)", conn);
da.InsertCommand.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = txtLastName.Text;
da.InsertCommand.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = txtFirstName.Text;
da.InsertCommand.Parameters.Add("@Title", SqlDbType.NVarChar).Value = txtTitle.Text;
da.InsertCommand.Parameters.Add("@Country", SqlDbType.NVarChar).Value = txtCountry .Text ;
da.InsertCommand.Parameters.Add("@City", SqlDbType.NVarChar).Value = txtCity .Text ;
da.InsertCommand.Parameters.Add("@BirthDate", SqlDbType.DateTime).Value =Convert .ToDateTime ( txtBirthDate.Text);
conn.Open();
da.InsertCommand.ExecuteNonQuery();
conn.Close();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
da.SelectCommand = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country,City,BirthDate from Employees",conn );
ds.Clear();
da.Fill(ds,"Employees");
dg.DataSource = ds.Tables["Employees"];
tblNamesBS .DataSource =ds.Tables ["Employees"];
txtFirstName.DataBindings.Clear();
txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
txtLastName.DataBindings.Clear();
txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "LastName"));
txtTitle.DataBindings.Clear();
txtTitle.DataBindings.Add(new Binding("Text", tblNamesBS, "Title"));
txtCountry.DataBindings.Clear();
txtCountry.DataBindings.Add(new Binding("Text", tblNamesBS, "Country"));
txtCity.DataBindings.Clear();
txtCity.DataBindings.Add(new Binding("Text", tblNamesBS, "City"));
txtBirthDate.DataBindings.Clear();
txtBirthDate.DataBindings.Add(new Binding("Text", tblNamesBS, "BirthDate"));
Records();
}
private void btnFirst_Click(object sender, EventArgs e)
{
tblNamesBS.MoveFirst();
dgUpdate();
Records();
}
private void btnPrevious_Click(object sender, EventArgs e)
{
tblNamesBS.MovePrevious();
dgUpdate();
Records();
}
private void btnNext_Click(object sender, EventArgs e)
{
tblNamesBS.MoveNext();
dgUpdate();
Records();
}
private void btnLast_Click(object sender, EventArgs e)
{
tblNamesBS.MoveLast();
dgUpdate();
Records();
}
private void dgUpdate()
{
dg.ClearSelection();
dg.Rows[tblNamesBS.Position].Selected = true;
Records();
}
private void Records()
{
label7.Text = "Record " + (tblNamesBS.Position + 1) + " of " + tblNamesBS.Count;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
da.UpdateCommand = new SqlCommand("Update Employees set lastname=@lastname,firstname=@firstname,title=@title,country=@country,city=@city,birthdate=@birthdate where EmployeeID=@ID",conn );
da.UpdateCommand.Parameters.Add("@lastname", SqlDbType.NVarChar).Value = txtLastName.Text;
da.UpdateCommand.Parameters.Add("@firstname", SqlDbType.NVarChar).Value = txtFirstName.Text;
da.UpdateCommand.Parameters.Add("@title", SqlDbType.NVarChar).Value = txtTitle.Text;
da.UpdateCommand.Parameters.Add("@country", SqlDbType.NVarChar).Value = txtCountry.Text;
da.UpdateCommand.Parameters.Add("@city", SqlDbType.NVarChar).Value = txtCity.Text;
da.UpdateCommand.Parameters.Add("@birthdate", SqlDbType.DateTime ).Value = Convert .ToDateTime (txtBirthDate.Text);
da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables["Employees"].Rows[tblNamesBS.Position][0];
conn.Open();
da.UpdateCommand.ExecuteNonQuery();
conn.Close();
}
private void btnDelete_Click(object sender, EventArgs e)
{
DialogResult dr = new DialogResult();
dr=MessageBox.Show("Are You Sure?\nThere is no undo once data is deleted", "Confirm Deletion", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
da.DeleteCommand = new SqlCommand("delete from Employees where EmployeeID=@ID and FirstName=@FirstName", conn);
da.DeleteCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables["Employees"].Rows[tblNamesBS.Position][0];
da.DeleteCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
conn.Open();
da.DeleteCommand.ExecuteNonQuery();
conn.Close();
}
else
{
MessageBox.Show("Deletion canceled");
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
da.SelectCommand = new SqlCommand("select firstname,lastname,title,country,city,birthdate from employees where lastname=@lastname and firstname=@firstname ",conn);
da.SelectCommand.Parameters.Add("@lastname", SqlDbType.NVarChar).Value = txtLastName.Text;
da.SelectCommand.Parameters.Add("@firstname", SqlDbType.NVarChar).Value = txtFirstName.Text;
conn.Open();
da.SelectCommand.ExecuteNonQuery();
ds.Clear();
da.Fill(ds, "Employees");
dg.DataSource = ds.Tables["Employees"];
conn.Close();
}
private void dg_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dg.Rows[tblNamesBS.Position].Selected = true;//Ovo nece radit kako treba
}
}
}