DataGridView
Since Visual Studio 2008 is due out by the end of the Month I am updating some of my datagridview samples for LINQ.
To start off create a new windows forms application in VS 2008 make sure you select FrameWork 3.5 so you can use linq
I now added a new Linq to Sql designer to the project and named it Northwind. Drag the Northwind Products Table on to the design surface from the Server Explorer.
On your windows forms add a DataGridView (DataGridView1) and a NumericUpDown control (nuPage). For this...
Here is a quick example on using an autocomplete combobox in the DataGridView. In this example I load all the possible values for the combobox into a AutoCompleteStringCollection and make that the DataGridViewComboBox's datasource. In the editingControl showing event you need to set the ComboBox's DropDownStyle, and the auto complete settings.
Imports System.Data.SqlClient
Public Class Form1
Dim scAutoComplete As New AutoCompleteStringCollection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
Dim da As SqlDataAdapter
Dim conn As SqlConnection
Dim ds As New DataSet
strConn = "Server = .\SQLEXPRESS;Database = NorthWind; Integrated Security = SSPI;"
conn...
I had some one ask me an interesting question about using linq with the datagridview
When I bind a datagridview to this query
Dim names() As String = {"hello11", "hello212", "hello123", "hello124", "hello2325", "hello336", "hello457"}
Dim query = From s In names _
Order By s _
Select s
Dim bs As New BindingSource
bs.DataSource = query
DataGridView1.DataSource = bs
Why do I get these results?
The answer the datagridview will show the properties of the class in the list bound to the datagridview. In this case we are bound to a list of string and the only bindable property is its Length. ...
Sql Endpoint and DataGridViewSQL Server 2005 allows you to create webservices for accessing the data in a database. These webservices are call SQL End Points. Lets start by creating a stored procedure to get the contact name and there titles for all the customers in the northwind database.
Create PROCEDURE [dbo].[GetContacts]
AS
BEGIN
SET NOCOUNT ON;
SELECT [ContactName],[ContactTitle] FROM Customers ORDER BY [ContactName];
END
Now we can create the end point. Note for this example I am using port 88 for the end point to prevent errors if you have IIS installed.
CREATE ENDPOINT NW_Contacts
STATE = Started
AS HTTP
(
PATH = '/Contacts',
AUTHENTICATION = (INTEGRATED),
PORTS = (CLEAR),
CLEAR_PORT = 88,
SITE = '*'
)
FOR SOAP
(
WEBMETHOD 'GetContacts'
(NAME...
Get XML string into a DataGridView
In the msdn forums I see people asking how do they get a string that contains xml to display in the DataGridView. They way to do this is to read the string into a StringReader and use the DataSet,Readxml method to convert the xml into a datatable. Note the xml must be well formed for this to work.
Dim srXML As New IO.StringReader(strXML)
Dim dsXML As New DataSet
dsXML.ReadXml(srXML)
DataGridView1.DataSource = dsXML.Tables(0)
Draw an Icon in a DataGridViewButtonCell
Here is a simple example on how to draw in a DataGridViewButtonCell. In this example an icon is displayed when button is pushed and the icon is made invisible when its pushed again. I am storing the if the button has been pressed in the cell's tag. I use the cellPainting event to draw the icon when needed.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CSButtonColumn
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String strConn = "Server = .\\SqlExpress;Database =...
Data From Multiple Tables in a DataGridView
There is Msdn Knowledge base article which shows how to create a JoinView class. The JoinView class is for joining 2 tables together for data binding. Basically you load 2 or more tables into a dataset and set up some data relations for the related tables. When you create the Joinview the first argument is the main table, 2nd is a list of fields you want to show, 3rd is a filter, and 4th is the field to sort on. The last 2 arguments are option. Since the article includes a vb sample I...
AutoComplete in DataGridView
In the datagridview's editing control showing event you have better access to the textboxes properties. Here is an example of adding autocomplete to the textbox.
VB Sample
Imports System.Data.SqlClient
Public Class Form1
Dim scAutoComplete As New AutoCompleteStringCollection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
Dim da As SqlDataAdapter
Dim conn As SqlConnection
Dim ds As New DataSet
strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * from [Orders]", conn)
da.Fill(ds, "Orders")
DataGridView1.DataSource = ds.Tables("Orders")
Dim cmd As New SqlCommand("Select...
Predicates
I recently read an article in the MSDN magazine about Predicates and Actions written by Ken Getz. I was thinking that predicates will make it easy to create a master details datagridview for a business object. This example creates 2 classes Customers and Orders. I used the Northwind database to fill a list of customers and a list of orders. I bound one grid to a binding source who's datasource is the list of customers. In the binding sources position changed event I use the list of orders findall method to show all the orders for a customer.
The classes...
Create a Pivot Table
Here is a quick example on how to rotate or pivot the data in a datagridview. This is a c# program which creates a new data table with a rotated version of the data to bind to. There is a VB version on the vb-tips website.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Pivot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dt1;
private void Form1_Load(object sender, EventArgs e)
{
dt1 = CreateTable();
DataTable dt2 = new DataTable("Reflected");
for (int i = 0; i < dt1.Rows.Count; i++)
{
dt2.Columns.Add(i.ToString());
...
Full DataGridView Archive