Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Code Snippets » Application windows, menus & toolbars »
Create your own Detail View in Windows Application
|
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. For some fields (Boolean values, and Currency), custom formatting is accomplished using the Format event of the Binding class.
Finally, the user can scroll through the records using the cursor keys. Right and left move to the next and previous records respectively, and home and end move to the first and last records. Page down/up skip forwards and backwards 10 records at a time. The information is retrieved into a Typed Dataset. This object offers a number of advantages over generic DataSets, which include simpler code, and less opportunity. I have taken here a Smaple od Products Catalog.
//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) { // Display a status message saying that you're attempting to connect. // This only needs to be done the very first time a connection is // attempted. frmStatus frmStatusMessage = new frmStatus();
if (!DidPreviouslyConnect) { frmStatusMessage.Show("Connecting to SQL Server"); }
bool IsConnecting = true;
while (IsConnecting) { try { SqlConnection Conn = new SqlConnection(Connectionstring);
// This select statement retrieves all the products, and looks up the // associated CategoryName, and SupplierName for each product.
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";
// The SqlDataAdapter will actually issue the command to the database.
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.KeyEventArgs e) { if (e.KeyCode == Keys.Right) { NextRecord();} if (e.KeyCode == Keys.Left) { PreviousRecord();} if (e.KeyCode == Keys.Home) { FirstRecord();} if (e.KeyCode == Keys.End) { LastRecord();} if (e.KeyCode == Keys.PageDown) { Forward10();} if (e.KeyCode == Keys.PageUp) { Back10();} }
Also attaching a sample design view of the form.
Attachments
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|