Locations of visitors to this page
Onteora Software - June 2006

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Binding a Datagridview and binding navigator with code

Binding a Datagridview and binding navigator with code



Here is a c# example on how to bind a datagridview and bindingnavigator with code. I manually added the save button to the bindingnavigator.





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CSharpBindingSource
{
    public partial class Form1 : Form
    {
        SqlDataAdapter da;
        SqlConnection conn;
        DataSet ds;
        BindingSource bs;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            String strConn = "Server = .;Database = NorthWind;Integrated Security = SSPI;";

            conn = new SqlConnection(strConn);
            da = new SqlDataAdapter("Select * From Employees", conn);
            SqlCommandBuilder cmd = new SqlCommandBuilder(da);
            ds = new DataSet();

            da.Fill(ds, "Employees");
            bs = new BindingSource(ds, "Employees");
            dataGridView1.DataSource = bs;
            bindingNavigator1.BindingSource = bs;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            bs.EndEdit();
            da.Update(ds, "Employees");
        }
    }
}

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: DataGridView | C#
Posted by Ken Tucker on Sunday, June 25, 2006 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Get the right row from a sorted DataGridView

Get the right row from a sorted DataGridView



When a datagridview is bound to a datatable you need to use the datatable's defautview to find the right record after the grid has been sorted.





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;
using System.Diagnostics;

namespace CsharpRow
{
    public partial class Form1 : Form
    {
        DataTable dt;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            String strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;";
            SqlConnection conn = new SqlConnection(strConn);
            SqlDataAdapter da = new SqlDataAdapter("Select * from Products", conn);
            dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource=dt;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataRowView dv;
            try
            {
                dv = dt.DefaultView[e.RowIndex];
                MessageBox.Show(dv["ProductName"].ToString());
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }

        }
    }
}

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: DataGridView | C#
Posted by Ken Tucker on Saturday, June 24, 2006 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Tallahassee Code Camp

Tallahassee Code Camp



I did a session on the DataGridView and DLinq at the Tallahassee code camp today. You can find the power point slides and sample code at the vb-tips website's new code camp section.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Camp
Posted by Ken Tucker on Saturday, June 17, 2006 11:42 AM
Permalink | Comments (0) | Post RSSRSS comment feed