Data. Grid. View in Windows Forms – Tips, Tricks and Frequently Asked Questions(FAQ)Data. Grid. View in Windows Forms – Tips, Tricks and Frequently Asked Questions(FAQ)Data. Grid. View control is a Windows Forms control that gives you the ability to customize and edit tabular data. It gives you number of properties, methods and events to customize its appearance and behavior. In this article, we will discuss some frequently asked questions and their solutions. These questions have been collected from a variety of sources including some newsgroups, MSDN site and a few, answered by me at the MSDN forums. Tip 1 – Populating a Data. Grid. View. In this short snippet, we will populate a Data. Grid. View using the Load. Data() method. This method uses the Sql. Data. Adapter to populate a Data. Set. The table ‘Orders’ in the Data. DataGridView control is a Windows Forms control that gives you the ability to customize and edit tabular data. It gives you number of properties, methods and events. Although LightSwitch doesn’t include a built-in report generator, you can build reports by using other products and integrate the results into your LightSwitch. Horse racing; Ice hockey; Karate; Olympics; Racing; Motorsport Cricket is a bat-and-ball game played between two teams of eleven players on an oval-shaped field. ![]() Set is then bound to the Binding. Source component which gives us the flexibility to choose/modify the data location. C#publicpartialclass. Form. 1 : Form { private. Sql. Data. Adapter da; private. Sql. Connection conn; Binding. ![]() Source bsource = new. Binding. Source(); Data. Set ds = null; string sql; public Form. Initialize. Component(); } privatevoid btn. Load_Click(object sender, Event. Args e) { Load. Data(); } privatevoid Load. Data() {string connection. String = "Data Source=localhost; Initial Catalog=Northwind; " + "Integrated Security=SSPI; "; conn = new. Sql. Connection(connection. String); sql = "SELECT Order. ID, Customer. ID, Employee. ID, Order. Date, Freight," + "Ship. Name, Ship. Country FROM Orders"; da = new. Sql. Data. Adapter(sql, conn); conn. Open(); ds = new. Data. Set(); Sql. Command. Builder command. Builder = new. Sql. Command. Builder(da); da. Fill(ds, "Orders"); bsource. Data. Source = ds. Tables["Orders"]; dgv. Data. Source = bsource; } } VB. NETPublic. Partial. Class Form. 1 Inherits Form Private da As Sql. Data. Adapter Private conn As Sql. Connection Private bsource As Binding. Source = New Binding. Source() Private ds As Data. Set = Nothing Private sql As. String Public. Sub. New() Initialize. Component() End. Sub. Private. Sub btn. Load_Click(By. Val sender As. Object, By. Val e As Event. Args) Load. Data() End. Sub Private. Sub Load. Data()Dim connection. String As. String = "Data Source=localhost; Initial Catalog=Northwind; " & "Integrated Security=SSPI; " conn = New Sql. Connection(connection. String)sql = "SELECT Order. ID, Customer. ID, Employee. ID, Order. Date, Freight," & "Ship. Name, Ship. Country FROM Orders" da = New Sql. Data. Adapter(sql, conn) conn. Open() ds = New Data. Set()Dim command. Builder As Sql. Command. Builder = New Sql. Command. Builder(da) da. Fill(ds, "Orders") bsource. Data. Source = ds. Tables("Orders") dgv. Data. Source = bsource End. Sub. End. Class. Tip 2 – Update the data in the Data. Grid. View and save changes in the database. After editing the data in the cells, if you would like to update the changes permanently in the database, use the following code: C# privatevoid btn. Update_Click(object sender, Event. Args e) { Data. Table dt = ds. Tables["Orders"]; this. Binding. Context[dt].End. Current. Edit(); this.Update(dt); }VB. more. NETPrivate. Sub btn. Update_Click(By. Val sender As. Object, By. Val e As Event.Args) Dim dt As Data. Counter Strike Aimbot And Wallhack Download Cs on this page. Table = ds. Tables("Orders") Me. Binding. Context(dt). End. Current. Edit() Me. Update(dt) End. Sub. Tip 3 – Display a confirmation box before deleting a row in the Data. Grid. View. Handle the User. Deleting. Row event to display a confirmation box to the user. If the user confirms the deletion, delete the row. If the user clicks cancel, set e. C#privatevoid dgv_User. Deleting. Row(object sender, Data. Grid. View. Row. Cancel. Event. Args e) { if (! Row. Is. New. Row) { Dialog. Result res = Message. Box. Show("Are you sure you want to delete this row?", "Delete confirmation", Message. Box. Buttons. Yes. No, Message. Box. Icon. Question); if (res == Dialog. Result. No) e. Cancel = true; } }VB. NETPrivate. Sub dgv_User. Deleting. Row(By. Val sender As. Object, By. Val e As Data. Grid. View. Row. Cancel. Event. Args) If (Not e. Row. Is. New. Row) Then Dim res As Dialog. Result = Message. Box. Show("Are you sure you want to delete this row?", "Delete confirmation", Message. Box. Buttons. Yes. No, Message. Box. Icon. Question) If res = Dialog. Result. No Then e. Cancel = True End. If End. If. End. Sub. Tip 4 – How to autoresize column width in the Data. Grid. View. The snippet shown below, first auto- resizes the columns to fit its content. Then the Auto. Size. Columns. Mode is set to the ‘Data. Grid. View. Auto. Size. Columns. Mode. All. Cells’ enumeration value which automatically adjust the widths of the columns when the data changes. C#privatevoid btn. Resize_Click(object sender, Event. Args e) { dgv. Auto. Resize. Columns(); dgv. Auto. Size. Columns. Mode = Data. Grid. View. Auto. Size. Columns. Mode. All. Cells; }VB. NETPrivate. Sub btn. Resize_Click(By. Val sender As. Object, By. Val e As Event. Args) dgv. Auto. Resize. Columns() dgv. Auto. Size. Columns. Mode = Data. Grid. View. Auto. Size. Columns. Mode. All. Cells. End. Sub. Tip 5 - Select and Highlight an entire row in Data. Grid. View. C#int row. To. Be. Selected = 3; // third rowif (dgv. Rows. Count > = row. To. Be. Selected){ // Since index is zero based, you have to subtract 1 dgv. Rows[row. To. Be. Selected - 1]. Selected = true; }VB. NETDim row. To. Be. Selected As. Integer = 3 ' third row. If dgv. Rows. Count > = row. To. Be. Selected Then ' Since index is zero based, you have to subtract 1 dgv. Rows(row. To. Be. Selected - 1). Selected = True.End. If. Tip 6 - How to scroll programmatically to a row in the Data.Grid. View. The Data. here. Grid. View has a property called First. Displayed. Scrolling. Row. Index that can be used in order to scroll to a row programmatically. C#int jump. To. Row = 2. Rows. Count > = jump. To. Row & & jump. To. Row > = 1){ dgv. First. Displayed. Scrolling. Row. Index = jump. To. Row; dgv. Rows[jump. To. Row]. Selected = true; }VB. NETDim jump. To. Row As. Integer = 2. 0If dgv. Rows. Count > = jump. To. Row And. Also jump. To. Row > = 1 Then dgv. First. Displayed. Scrolling. Row. Index = jump. To. Row dgv. Rows(jump. To. Row). Selected = True. End. If. Tip 7 - Calculate a column total in the Data. Grid. View and display in a textbox A common requirement is to calculate the total of a currency field and display it in a textbox. In the snippet below, we will be calculating the total of the ‘Freight’ field. We will then display the data in a textbox by formatting the result (observe the To. String("c")) while displaying the data, which displays the culture- specific currency. C#privatevoid btn. Total_Click(object sender, Event. Args e) { if(dgv. Rows. Count > 0) txt. Total. Text = Total(). To. String("c"); } privatedouble Total() { double tot = 0; int i = 0; for (i = 0; i < dgv. Rows. Count; i++) { tot = tot + Convert. To. Double(dgv. Rows[i]. Cells["Freight"]. Value); } return tot; }VB. NETPrivate. Sub btn. Total_Click(By. Val sender As. Object, By. Val e As Event. Args) If dgv. Rows. Count > 0 Then txt. Total. Text = Total(). To. String("c") End. If. End. Sub. Private. Function Total() As. Double Dim tot As. Double = 0 Dim i As. Integer = 0 For i = 0 To dgv. Rows. Count - 1 tot = tot + Convert. To. Double(dgv. Rows(i). Cells("Freight").
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |