public partial class Form1 : Form
{
private const string _CommandText =
"SELECT authors.au_id, au_lname, au_fname, " +
"titles.title_id, title, price " +
"FROM authors " +
"JOIN titleauthor ON authors.au_id = " +
"titleauthor.au_id " +
"JOIN titles ON titleauthor.title_id = " +
"titles.title_id " +
"ORDER BY au_lname, au_fname";
private const string _ConnectionString =
"server=localhost;database=pubs;integrated security=true";
SqlConnection objConnection;
SqlDataAdapter objDataAdapter;
DataSet objDataSet;
DataView objDataView;
CurrencyManager objCurrencyManager;
public Form1()
{
objConnection = new SqlConnection(_ConnectionString);
objDataAdapter = new SqlDataAdapter(_CommandText, objConnection);
InitializeComponent();
}
private void FillDataSetAndView()
{
objDataSet =new DataSet();
objDataAdapter.Fill(objDataSet,"authors");
objDataView=new DataView(objDataSet.Tables["authors"]);
objCurrencyManager =(CurrencyManager) this.BindingContext[objDataView];
}
private void BindFields()
{
txtLastName.DataBindings.Clear();
txtFirstName.DataBindings.Clear();
txtBookTitle.DataBindings.Clear();
txtPrice.DataBindings.Clear();
txtLastName.DataBindings.Add("Text", objDataView, "au_lname");
txtFirstName.DataBindings.Add("Text", objDataView, "au_fname");
txtBookTitle.DataBindings.Add("Text", objDataView, "title");
txtPrice.DataBindings.Add("Text", objDataView, "price");
toolStripStatusLabel1.Text = "Ready";
}
private void ShowPosition()
{
try
{
txtPrice.Text = Decimal.Parse(txtPrice.Text).ToString("##0.00");
}
catch (System.Exception e)
{
txtPrice.Text = "0";
txtPrice.Text=Decimal.Parse(txtPrice.Text).ToStrin g("##0.00");
}
txtRecordPosition.Text = (objCurrencyManager.Position + 1)+
"of" + objCurrencyManager.Count;
}
private void Form1_Load(object sender, EventArgs e)
{
cboField.Items.Add("نام خانوادگی");
cboField.Items.Add("نام");
cboField.Items.Add("عنوان کتاب");
cboField.Items.Add("قیمت");
cboField.SelectedIndex = 0;
FillDataSetAndView();
BindFields();
ShowPosition();
}
private void btnMoveFirst_Click(object sender, EventArgs e)
{
objCurrencyManager.Position = 0;
ShowPosition();
}
private void btnMovePrevious_Click(object sender, EventArgs e)
{
objCurrencyManager.Position -= 1;
ShowPosition();
}
private void btnMoveNext_Click(object sender, EventArgs e)
{
objCurrencyManager.Position += 1;
ShowPosition();
}
private void btnMoveLast_Click(object sender, EventArgs e)
{
objCurrencyManager.Position = objCurrencyManager.Count - 1;
ShowPosition();
}
private void btnPerformSort_Click(object sender, EventArgs e)
{
switch (cboField.SelectedIndex)
{
case 0:
objDataView.Sort="au_lname";
break;
case 1:
objDataView.Sort="au_fname";
break;
case 2:
objDataView.Sort = "title";
break;
case 3:
objDataView.Sort = "price";
break;
}
btnMoveFirst_Click(null,null);
toolStripStatusLabel1.Text = "مرتب سازی انجام شد";
}
private void btnPerformSearch_Click(object sender, EventArgs e)
{
int intposition;
switch (cboField.SelectedIndex)
{
case 0:
objDataView.Sort = "au_lname";
break;
case 1:
objDataView.Sort = "au_fname";
break;
case 2:
objDataView.Sort = "title";
break;
case 3:
objDataView.Sort = "price";
break;
}
if (cboField.SelectedIndex < 3)
{
intposition = objDataView.Find(txtSearchCriteria.Text);
}
else
{
intposition = objDataView.Find(Decimal.Parse(txtSearchCriteria.T ext));
}
if (intposition == -1)
{
toolStripStatusLabel1.Text = "موردی یافت نشد";
}
else
{
toolStripStatusLabel1.Text = "جستجو با موفقیت انجام شد ";
objCurrencyManager.Position = intposition;
}
ShowPosition();
}
private void btnNew_Click(object sender, EventArgs e)
{
txtBookTitle.Text = "";
txtPrice.Text = "";
}
}
ارادتمند