M
Mike P
I've just coded my first data grid related piece of code. I am
populating a datagrid via a dataset. What I want to know is because
datasets deal with disconnected data, is there a quick way to refresh
your data (like a Refresh method for example). Or do you have to go
through exactly the same method that you used to populate in the first
place again?
Thanks,
Mike
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.DataGrid dataGrid1;
private SqlConnection connNumberRanges;
private SqlDataAdapter daaLocation;
private SqlCommand comLocation;
private DataSet dasLocation;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginIni
t();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(16, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(160, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 56);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(472, 208);
this.dataGrid1.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 273);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Win Forms (read-only)";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(
);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
string strConnection = "server=10.0.10.152; database=CCnS;
uid=admin2; pwd=donkey5man; Persist Security Info=False; Network
Library=dbmssocn";
connNumberRanges = new SqlConnection(strConnection);
comLocation = new SqlCommand("SELECT DISTINCT LOCATION FROM
NUMBER_RANGES", connNumberRanges);
daaLocation = new SqlDataAdapter("SELECT * FROM NUMBER_RANGES",
connNumberRanges);
dasLocation = new DataSet();
SqlDataReader darLocation;
connNumberRanges.Open();
darLocation = comLocation.ExecuteReader();
while (darLocation.Read())
{
comboBox1.Items.Add(darLocation[0].ToString());
}
darLocation.Close();
daaLocation.Fill(dasLocation, "Number_Ranges");
}
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
DataView objDataView = new
DataView(dasLocation.Tables["Number_Ranges"],
"Location = '" + comboBox1.Text.ToString() + "'",
"Location",
DataViewRowState.CurrentRows);
dataGrid1.DataSource = objDataView;
}
}
}
populating a datagrid via a dataset. What I want to know is because
datasets deal with disconnected data, is there a quick way to refresh
your data (like a Refresh method for example). Or do you have to go
through exactly the same method that you used to populate in the first
place again?
Thanks,
Mike
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.DataGrid dataGrid1;
private SqlConnection connNumberRanges;
private SqlDataAdapter daaLocation;
private SqlCommand comLocation;
private DataSet dasLocation;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginIni
t();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(16, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(160, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 56);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(472, 208);
this.dataGrid1.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 273);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Win Forms (read-only)";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(
);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
string strConnection = "server=10.0.10.152; database=CCnS;
uid=admin2; pwd=donkey5man; Persist Security Info=False; Network
Library=dbmssocn";
connNumberRanges = new SqlConnection(strConnection);
comLocation = new SqlCommand("SELECT DISTINCT LOCATION FROM
NUMBER_RANGES", connNumberRanges);
daaLocation = new SqlDataAdapter("SELECT * FROM NUMBER_RANGES",
connNumberRanges);
dasLocation = new DataSet();
SqlDataReader darLocation;
connNumberRanges.Open();
darLocation = comLocation.ExecuteReader();
while (darLocation.Read())
{
comboBox1.Items.Add(darLocation[0].ToString());
}
darLocation.Close();
daaLocation.Fill(dasLocation, "Number_Ranges");
}
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
DataView objDataView = new
DataView(dasLocation.Tables["Number_Ranges"],
"Location = '" + comboBox1.Text.ToString() + "'",
"Location",
DataViewRowState.CurrentRows);
dataGrid1.DataSource = objDataView;
}
}
}