C# express addition DataRow related warning

  • Thread starter Thread starter michael_geller
  • Start date Start date
M

michael_geller

While experimenting with binding datasets to an MS DataGridView, I get
a warning that tells me that my DataRows are "either undeclared or was
never assigned". Assuming that I am reading the documentation
correctly, you are supposed to create a DataRow using the
"DataTable.NewRow()" method. This is what I do in my app. As a matter
of fact, the DataRow constructor appears to be protected so I can't
directly create it if I want to. Is this warning a bug in MS C#
express? Or am I doing something wrong? If I ignore the warning and
run the app, it works as expected. Just trying to sort out the best
way to remove this warning.

-Michael
 
Michael,

Can you post the code? This error has little to do with the actual way
you create the data row, but rather, a variable that is not assigned
properly.

Hope this helps.
 
While experimenting with binding datasets to an MS DataGridView, I get
a warning that tells me that my DataRows are "either undeclared or was
never assigned". Assuming that I am reading the documentation
correctly, you are supposed to create a DataRow using the
"DataTable.NewRow()" method. This is what I do in my app. As a matter
of fact, the DataRow constructor appears to be protected so I can't
directly create it if I want to. Is this warning a bug in MS C#
express? Or am I doing something wrong? If I ignore the warning and
run the app, it works as expected. Just trying to sort out the best
way to remove this warning.

-Michael
You create a row by doing:

DataRow dr = dtDataTable.NewRow();

Nut after that you need to add that row to the table:

dtDataTable.Rows.Add(dr);

....because beleive it or not, the row created by NewRow doesn't belong to the table by default.

Hope it helps,
MuZZy
 
Here is the code. I know that it isn't good practice to put data
related code directly together with GUI code. This was just a simple
test to familiarize myself with the MS data related classes; search for
the string "DataRow dr = d.NewRow();" to jump to relavent section of
the code; I think I'm doing things by the book as per MS documentation,
but perhaps I'm missing something such that I'm getting the warning in
C# Express Edition; note that the code compiles and runs fine...
----------------------------------------------

using System.Data;

namespace Test
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.d = new System.Data.DataTable();
this.ds = new System.Data.DataSet();
this.dataGridView1 = new
System.Windows.Forms.DataGridView();
this.button1 = new System.Windows.Forms.Button();
this.clientIDDataGridViewTextBoxColumn = new
System.Windows.Forms.DataGridViewTextBoxColumn();
this.clientNameDataGridViewTextBoxColumn = new
System.Windows.Forms.DataGridViewTextBoxColumn();

((System.ComponentModel.ISupportInitialize)(this.d)).BeginInit();

((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// d
//
this.d.TableName = "Client";
//
// ds
//
this.ds.DataSetName = "ClientDB";
this.ds.Tables.AddRange(new System.Data.DataTable[] {
this.d});

d.Columns.Add("ClientID",
System.Type.GetType("System.Int32"));
d.Columns.Add("ClientName",
System.Type.GetType("System.String"));

DataRow dr = d.NewRow();
dr["ClientID"] = 2;
dr["ClientName"] = "Fidelity";
d.Rows.Add(dr);

//
// dataGridView1
//
this.dataGridView1.AllowUserToOrderColumns = true;
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new
System.Windows.Forms.DataGridViewColumn[] {
this.clientIDDataGridViewTextBoxColumn,
this.clientNameDataGridViewTextBoxColumn});
this.dataGridView1.DataMember = "Client";
this.dataGridView1.DataSource = this.ds;
this.dataGridView1.Location = new System.Drawing.Point(139,
69);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(240,
150);
this.dataGridView1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(457, 71);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(154, 72);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// clientIDDataGridViewTextBoxColumn
//
this.clientIDDataGridViewTextBoxColumn.DataPropertyName =
"ClientID";
this.clientIDDataGridViewTextBoxColumn.HeaderText =
"ClientID";
this.clientIDDataGridViewTextBoxColumn.Name =
"clientIDDataGridViewTextBoxColumn";
//
// clientNameDataGridViewTextBoxColumn
//
this.clientNameDataGridViewTextBoxColumn.DataPropertyName =
"ClientName";
this.clientNameDataGridViewTextBoxColumn.HeaderText =
"ClientName";
this.clientNameDataGridViewTextBoxColumn.Name =
"clientNameDataGridViewTextBoxColumn";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(834, 663);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";

((System.ComponentModel.ISupportInitialize)(this.d)).EndInit();

((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button1;
private System.Data.DataTable d;
private System.Data.DataSet ds;
private System.Windows.Forms.DataGridViewTextBoxColumn
clientIDDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn
clientNameDataGridViewTextBoxColumn;
}
}
 
Back
Top