Click here to Skip to main content
15,920,896 members
Home / Discussions / C#
   

C#

 
GeneralRe: GDI+ usercontrol problem Pin
Heath Stewart30-Oct-04 2:22
protectorHeath Stewart30-Oct-04 2:22 
Generalbind datagrid to a variable Pin
xiaowenjie28-Oct-04 15:29
xiaowenjie28-Oct-04 15:29 
GeneralRe: bind datagrid to a variable Pin
Heath Stewart28-Oct-04 16:13
protectorHeath Stewart28-Oct-04 16:13 
GeneralRe: bind datagrid to a variable Pin
xiaowenjie28-Oct-04 19:08
xiaowenjie28-Oct-04 19:08 
GeneralRe: bind datagrid to a variable Pin
Heath Stewart29-Oct-04 5:38
protectorHeath Stewart29-Oct-04 5:38 
GeneralRe: bind datagrid to a variable Pin
xiaowenjie29-Oct-04 6:15
xiaowenjie29-Oct-04 6:15 
GeneralRe: bind datagrid to a variable Pin
xiaowenjie29-Oct-04 6:29
xiaowenjie29-Oct-04 6:29 
GeneralRe: bind datagrid to a variable Pin
Heath Stewart30-Oct-04 2:58
protectorHeath Stewart30-Oct-04 2:58 
I'm not sure if you're posting just snippets from various methods or if this is your entire method. If the latter is true, then of course it won't work. You need to declare myVariable (and I certainly hope you haven't given it that non-descript name) as a field (so you don't loose it after setting it) and put everything below that in your code snippet in an event handler for DataGrid.RowHeaderClick (which means you'll have to extend DataGrid with your own control since RowHeaderClick is protected), or just handle the Click event, like so:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
 
public class Form1 : Form
{
  private DataGrid dataGrid1;
  private Label label1;
  private SqlCommand sqlSelectCommand1;
  private SqlConnection sqlConnection1;
  private SqlDataAdapter sqlDataAdapter1;
  private DataView dataView1;
 
  public Form1()
  {
    InitializeComponent();
  }
 
  protected override void OnLoad(EventArgs e)
  {
    DataSet ds = new DataSet();
    sqlDataAdapter1.Fill(ds);
 
    dataView1 = new DataView(ds.Tables[0]);
    dataView1.AllowDelete = false;
    dataView1.AllowEdit = false;
    dataView1.AllowNew = false;
    dataGrid1.DataSource = dataView1;
  }
 
  private void InitializeComponent()
  {
    this.dataGrid1 = new DataGrid();
    this.label1 = new Label();
    this.sqlSelectCommand1 = new SqlCommand();
    this.sqlConnection1 = new SqlConnection();
    this.sqlDataAdapter1 = new SqlDataAdapter();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) 
      | AnchorStyles.Left) 
      | AnchorStyles.Right)));
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(8, 8);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(280, 208);
    this.dataGrid1.TabIndex = 0;
    this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
    // 
    // label1
    // 
    this.label1.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) 
      | AnchorStyles.Right)));
    this.label1.Location = new System.Drawing.Point(8, 224);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(280, 23);
    this.label1.TabIndex = 1;
    // 
    // sqlSelectCommand1
    // 
    this.sqlSelectCommand1.CommandText = "SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice" +
      ", UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued, CategoryName FROM [Alp" +
      "habetical list of products]";
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "workstation id=ALICE;packet size=4096;integrated security=SSPI;data source=\".\";pe" +
      "rsist security info=False;initial catalog=Northwind";
    // 
    // sqlDataAdapter1
    // 
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.TableMappings.AddRange(new DataTableMapping[] {
      new DataTableMapping("Table", "Alphabetical list of products", new DataColumnMapping[] {
        new DataColumnMapping("ProductID", "ProductID"),
        new DataColumnMapping("ProductName", "ProductName"),
        new DataColumnMapping("SupplierID", "SupplierID"),
        new DataColumnMapping("CategoryID", "CategoryID"),
        new DataColumnMapping("QuantityPerUnit", "QuantityPerUnit"),
        new DataColumnMapping("UnitPrice", "UnitPrice"),
        new DataColumnMapping("UnitsInStock", "UnitsInStock"),
        new DataColumnMapping("UnitsOnOrder", "UnitsOnOrder"),
        new DataColumnMapping("ReorderLevel", "ReorderLevel"),
        new DataColumnMapping("Discontinued", "Discontinued"),
        new DataColumnMapping("CategoryName", "CategoryName")})});
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(296, 253);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.dataGrid1);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false);
 
  }
 
  static void Main() 
  {
    Application.Run(new Form1());
  }
 
  private void dataGrid1_Click(object sender, EventArgs e)
  {
    CurrencyManager cm = (CurrencyManager)dataGrid1.BindingContext[
      dataGrid1.DataSource, dataGrid1.DataMember];
    if (cm != null)
    {
      DataRowView view = cm.Current as DataRowView;
      if (view != null)
        label1.Text = view[1].ToString();
    }
  }
}


This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralCOM Interopability problem Pin
iliyang28-Oct-04 10:57
iliyang28-Oct-04 10:57 
GeneralRe: COM Interopability problem Pin
Heath Stewart28-Oct-04 16:04
protectorHeath Stewart28-Oct-04 16:04 
QuestionKeeping context menu open even after clicking? Pin
Carl Mercier28-Oct-04 9:29
Carl Mercier28-Oct-04 9:29 
AnswerRe: Keeping context menu open even after clicking? Pin
Heath Stewart28-Oct-04 15:58
protectorHeath Stewart28-Oct-04 15:58 
GeneralRe: Keeping context menu open even after clicking? Pin
Carl Mercier29-Oct-04 3:50
Carl Mercier29-Oct-04 3:50 
GeneralRe: Keeping context menu open even after clicking? Pin
LongRange.Shooter29-Oct-04 6:50
LongRange.Shooter29-Oct-04 6:50 
AnswerRe: Keeping context menu open even after clicking? Pin
perlmunger29-Oct-04 9:09
perlmunger29-Oct-04 9:09 
Generalabout device instance enumeration Pin
momer28-Oct-04 9:07
momer28-Oct-04 9:07 
GeneralRe: about device instance enumeration Pin
Heath Stewart28-Oct-04 15:53
protectorHeath Stewart28-Oct-04 15:53 
GeneralRegistry problem..! Pin
QzRz28-Oct-04 8:03
QzRz28-Oct-04 8:03 
GeneralRe: Registry problem..! Pin
Christian Graus28-Oct-04 9:39
protectorChristian Graus28-Oct-04 9:39 
GeneralRe: Registry problem..! Pin
QzRz28-Oct-04 9:45
QzRz28-Oct-04 9:45 
GeneralRe: Registry problem..! Pin
Christian Graus28-Oct-04 9:48
protectorChristian Graus28-Oct-04 9:48 
GeneralRe: Registry problem..! Pin
Alex Korchemniy28-Oct-04 10:04
Alex Korchemniy28-Oct-04 10:04 
GeneralRe: Registry problem..! Pin
Dave Kreskowiak28-Oct-04 16:49
mveDave Kreskowiak28-Oct-04 16:49 
GeneralRe: Registry problem..! Pin
QzRz29-Oct-04 6:19
QzRz29-Oct-04 6:19 
GeneralRe: Registry problem..! Pin
Dave Kreskowiak29-Oct-04 11:34
mveDave Kreskowiak29-Oct-04 11:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.