Create your own Detail View in Windows Application

Bubbly
Posted by Bubbly under C# category on | Views : 1776
This example shows how to create a typical data bound form. The information is displayed in a series of text boxes. As the user scrolls through the records, the text in the text boxes changes.

Featured Highlights
This sample shows how to data bind individual fields to controls. The sample also illustrates scrolling through the records using the typical first, previous,next, and last controls. In addition, the current record number, and total number of records, are displayed in a label.
//Product xsd
protected ProductDataSet productInfo = new ProductDataSet();
//Connection string
protected const string SQL_CONNECTION_STRING = "Your connection string here";
static string Connectionstring = SQL_CONNECTION_STRING;
static bool DidPreviouslyConnect = false;

private void frmMain_Load(object sender, System.EventArgs e)
{
frmStatus frmStatusMessage = new frmStatus();
if (!DidPreviouslyConnect)
{
frmStatusMessage.Show("Connecting to SQL Server");
}
bool IsConnecting = true;
while (IsConnecting)
{
try
{
SqlConnection Conn = new SqlConnection(Connectionstring);
string selectCommand = "SELECT Products.ProductID, Products.ProductName,Products.SupplierID, Products.CategoryID,Products.QuantityPerUnit, Products.UnitPrice,Products.UnitsInStock, Products.UnitsOnOrder,Products.ReorderLevel, Products.Discontinued, Suppliers.CompanyName AS SupplierName,Categories.CategoryName FROM Products INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID";

SqlDataAdapter productAdapter = new SqlDataAdapter(selectCommand, Conn);

productAdapter.Fill(productInfo.Products);
txtProductID.DataBindings.Add("Text", productInfo.Products, "ProductID");
txtProductName.DataBindings.Add("Text", productInfo.Products, "ProductName");
txtSupplier.DataBindings.Add("Text", productInfo.Products, "SupplierName");
txtCategory.DataBindings.Add("Text", productInfo.Products, "CategoryName");
txtQuantityPerUnit.DataBindings.Add("Text", productInfo.Products, "QuantityPerUnit");
txtUnitsInStock.DataBindings.Add("Text", productInfo.Products, "UnitsInStock");
txtUnitsOnOrder.DataBindings.Add("Text", productInfo.Products, "UnitsOnOrder");
txtReorderLevel.DataBindings.Add("Text", productInfo.Products, "ReorderLevel");
Binding UnitPriceBinding = new Binding("Text", productInfo.Products, "UnitPrice");
UnitPriceBinding.Format += new System.Windows.Forms.ConvertEventHandler(DecimalToCurrencystring);
txtUnitPrice.DataBindings.Add(UnitPriceBinding);
Binding DiscontinuedBinding =new Binding("Text", productInfo.Products, "Discontinued");
DiscontinuedBinding.Format += new System.Windows.Forms.ConvertEventHandler(boolToYesNo);
txtDiscontinued.DataBindings.Add(DiscontinuedBinding);
this.BindingContext[productInfo.Products].PositionChanged += new System.EventHandler(ProductInfo_PositionChanged);
// Data has been successfully retrieved, so break out of the loop.
IsConnecting = false;
DidPreviouslyConnect = true;
}
catch
{ //message here }
}
frmStatusMessage.Close();
ShowCurrentRecord();
}

When databinding to a boolean value, this converts the true/false to a yes/no string.
protected void boolToYesNo(object sender, ConvertEventArgs e)
{
// The method converts only to string type. Test this using the DesiredType.
if (e.DesiredType != typeof(string))
{ return; }

// if the value is "true", convert to "Yes", otherwise "No"
if (((bool)(e.Value)) == true)
{ e.Value = "Yes"; }
else
{ e.Value = "No"; }
}

protected void DecimalToCurrencystring(object sender, ConvertEventArgs e)
{
// The method converts only to string type. Test this using the DesiredType.
if (e.DesiredType != typeof(string))
{ return; }
// Use the Tostring method to format the value currency ("c").
e.Value = ((Decimal)(e.Value)).ToString("c");
}
For first,previous,next,last function
// Move Back 10 records
public void Back10()
{
// The position of the binding context controls the "current record"
this.BindingContext[productInfo.Products].Position -= 10;
}
// Move to the first record
public void FirstRecord()
{
// The position of the binding context controls the "current record" Position the first record is record 0 (! 1).
this.BindingContext[productInfo.Products].Position = 0;
}
// Move forward 10 records
public void Forward10()
{
// The position of the binding context controls the "current record"
this.BindingContext[productInfo.Products].Position += 10;
}
// Move to the last record
public void LastRecord()
{
// The position of the binding context controls the "current record". Use productInfo.Products.Count to figure out the total number of records. -1 because position is zero based.
this.BindingContext[productInfo.Products].Position = productInfo.Products.Count - 1;
}
// Move to the next record
public void NextRecord()
{
// The position of the binding context controls the "current record"
this.BindingContext[productInfo.Products].Position += 1;
}
// Move to the previous record
public void PreviousRecord()
{
// The position of the binding context controls the "current record"
this.BindingContext[productInfo.Products].Position -= 1;
}
// Output the number of the current record
protected void ShowCurrentRecord()
{
// The position of the binding context contains the current record. +1 so that the first record displays record 1 (instead of 0). productInfo.Products.Count gives the total number of records.

lblRecordNumber.Text = "Record " + (this.BindingContext[productInfo.Products].Position + 1).ToString() + " of " + productInfo.Products.Count;
}

private void btnFirst_Click(object sender, System.EventArgs e)
{
// Move to the first record
FirstRecord();
}

private void btnLast_Click(object sender, System.EventArgs e)
{
// Move to the last record
LastRecord();
}

private void btnNext_Click(object sender, System.EventArgs e)
{
// Move to the next record
NextRecord();
}

private void btnPrevious_Click(object sender, System.EventArgs e)
{
// Move to the previous record
PreviousRecord();
}


Let the user scroll through the records using the cursor keys. Left and right are next and previous. Home and end are first and last.
private void frmMain_KeyDown(object sender, System.Windows.Forms.KeyEve    
                    
            

Comments or Responses

Login to post response