473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

datagrid datatable problem.

At added two column and more column hereafter,I immediately click del
button,the program is no problem.

but if i first click the DataGrid columnHeader and then click the del
button, i find when column is

finally ,a error is happen

underside is my program.



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace deldatagridcolumn

{
public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;

private System.Windows.Forms.DataGrid grid;

DataTable dt = new DataTable("T");

public Form1()

{
InitializeComponent();
}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë
private void InitializeComponent()

{

this.grid = new System.Windows.Forms.DataGrid();

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.g rid)).BeginInit();

this.SuspendLayout();

//

// grid

//

this.grid.DataMember = "";

this.grid.HeaderForeColor =
System.Drawing.SystemColors.ControlText;

this.grid.Location = new System.Drawing.Point(24, 16);

this.grid.Name = "grid";

this.grid.Size = new System.Drawing.Size(448, 288);

this.grid.TabIndex = 0;

//

// button1

//

this.button1.Location = new System.Drawing.Point(304, 312);

this.button1.Name = "button1";

this.button1.TabIndex = 1;

this.button1.Text = "Add";

this.button1.Click += new
System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(400, 312);

this.button2.Name = "button2";

this.button2.TabIndex = 2;

this.button2.Text = "Del";

this.button2.Click += new
System.EventHandler(this.button2_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(504, 341);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Controls.Add(this.grid);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.g rid)).EndInit();

this.ResumeLayout(false);

}

#endregion


[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

DataColumn dc = new DataColumn("dcode",typeof(string));

dt.Columns.Add(dc);

DataRow dr = dt.NewRow();

dr[0] = "aa";

dt.Rows.Add(dr);

grid.DataSource = dt;

DataGridTableStyle myGridStyle = new DataGridTableStyle();

myGridStyle.MappingName = "T";

DataGridTextBoxColumn style = new DataGridTextBoxColumn();

style.MappingName = "dcode";

style.HeaderText = "DCODE";

style.Width = 100;

myGridStyle.GridColumnStyles.Add(style);

grid.TableStyles.Add(myGridStyle);

}

private void button1_Click(object sender, System.EventArgs e)

{
string FieldEn = "";

int i=0;

int j=0;

string caption = "";

while(FieldEn.Length==0)

{


for(j=0;j<dt.Columns.Count;j++)

{

if(("F" + i.ToString()) == dt.Columns[j].ColumnName)

{

break;

}

}


if(j==dt.Columns.Count)

{

FieldEn = "F" + i.ToString();

caption = "f" + i.ToString();

break;

}

i++;

}

DataColumn column = new DataColumn(FieldEn,typeof(string));

column.Caption = caption;

dt.Columns.Add(column);

DataGridTextBoxColumn myGridStyle = new
DataGridTextBoxColumn();

myGridStyle.MappingName = column.ColumnName;

myGridStyle.HeaderText = column.Caption;

myGridStyle.Width = 100;

grid.TableStyles[0].GridColumnStyles.Add(myGridStyle);

}

private void button2_Click(object sender, System.EventArgs e)

{

if(dt.Columns.Count > 1)

{

dt.Columns.RemoveAt(1);

grid.TableStyles[0].GridColumnStyles.RemoveAt(1);

}

}

}

}



Nov 16 '05 #1
3 1621
can you tell me a little more about the problem.

Basicaly, you created a datatable and added some datacolumns. If you fill
the datatable, bind it to the grid and try to delete a row, everything is
fine. However if you sort the grid first and then delete a column - you get
an exception? If this is correct, can you tell me what exception you are
getting? does teh exception occur when you hit the delete button or does it
happen when you try to update the db (ie call .Update method of the
dataadapter?). Could you post teh code you are using for the delete?

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/...ity/newsgroups
"clyyy2002" <cl*******@yahoo.com.cn> wrote in message
news:uj*************@TK2MSFTNGP11.phx.gbl...
At added two column and more column hereafter,I immediately click del
button,the program is no problem.

but if i first click the DataGrid columnHeader and then click the del
button, i find when column is

finally ,a error is happen

underside is my program.



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace deldatagridcolumn

{
public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;

private System.Windows.Forms.DataGrid grid;

DataTable dt = new DataTable("T");

public Form1()

{
InitializeComponent();
}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë
private void InitializeComponent()

{

this.grid = new System.Windows.Forms.DataGrid();

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.g rid)).BeginInit();

this.SuspendLayout();

//

// grid

//

this.grid.DataMember = "";

this.grid.HeaderForeColor =
System.Drawing.SystemColors.ControlText;

this.grid.Location = new System.Drawing.Point(24, 16);

this.grid.Name = "grid";

this.grid.Size = new System.Drawing.Size(448, 288);

this.grid.TabIndex = 0;

//

// button1

//

this.button1.Location = new System.Drawing.Point(304, 312);

this.button1.Name = "button1";

this.button1.TabIndex = 1;

this.button1.Text = "Add";

this.button1.Click += new
System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(400, 312);

this.button2.Name = "button2";

this.button2.TabIndex = 2;

this.button2.Text = "Del";

this.button2.Click += new
System.EventHandler(this.button2_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(504, 341);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Controls.Add(this.grid);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.g rid)).EndInit();

this.ResumeLayout(false);

}

#endregion


[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

DataColumn dc = new DataColumn("dcode",typeof(string));

dt.Columns.Add(dc);

DataRow dr = dt.NewRow();

dr[0] = "aa";

dt.Rows.Add(dr);

grid.DataSource = dt;

DataGridTableStyle myGridStyle = new DataGridTableStyle();

myGridStyle.MappingName = "T";

DataGridTextBoxColumn style = new DataGridTextBoxColumn();

style.MappingName = "dcode";

style.HeaderText = "DCODE";

style.Width = 100;

myGridStyle.GridColumnStyles.Add(style);

grid.TableStyles.Add(myGridStyle);

}

private void button1_Click(object sender, System.EventArgs e)

{
string FieldEn = "";

int i=0;

int j=0;

string caption = "";

while(FieldEn.Length==0)

{


for(j=0;j<dt.Columns.Count;j++)

{

if(("F" + i.ToString()) == dt.Columns[j].ColumnName)
{

break;

}

}


if(j==dt.Columns.Count)

{

FieldEn = "F" + i.ToString();

caption = "f" + i.ToString();

break;

}

i++;

}

DataColumn column = new DataColumn(FieldEn,typeof(string));

column.Caption = caption;

dt.Columns.Add(column);

DataGridTextBoxColumn myGridStyle = new
DataGridTextBoxColumn();

myGridStyle.MappingName = column.ColumnName;

myGridStyle.HeaderText = column.Caption;

myGridStyle.Width = 100;

grid.TableStyles[0].GridColumnStyles.Add(myGridStyle);

}

private void button2_Click(object sender, System.EventArgs e)

{

if(dt.Columns.Count > 1)

{

dt.Columns.RemoveAt(1);

grid.TableStyles[0].GridColumnStyles.RemoveAt(1);

}

}

}

}


Nov 16 '05 #2
underside is my new program.

you sort the grid (column caption is "DNAME" ) first and then click "del"
button, a exception occur(System.IndexOutOfRangeException) .


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace deldatagridcolumn
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
private System.Windows.Forms.DataGrid grid;

DataTable dt = new DataTable("T");
public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.grid = new System.Windows.Forms.DataGrid();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.g rid)).BeginInit();
this.SuspendLayout();
//
// grid
//
this.grid.DataMember = "";
this.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.grid.Location = new System.Drawing.Point(24, 16);
this.grid.Name = "grid";
this.grid.Size = new System.Drawing.Size(448, 288);
this.grid.TabIndex = 0;
//
// button2
//
this.button2.Location = new System.Drawing.Point(400, 312);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "Del";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 341);
this.Controls.Add(this.button2);
this.Controls.Add(this.grid);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.g rid)).EndInit();
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

DataColumn dc = new DataColumn("dcode",typeof(string));
DataColumn dc1 = new DataColumn("dname",typeof(string));
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
DataRow dr = dt.NewRow();
dr[0] = "aa";
dr[1] = "aa";
dt.Rows.Add(dr);

grid.DataSource = dt;

DataGridTableStyle myGridStyle = new DataGridTableStyle();
myGridStyle.MappingName = "T";
DataGridTextBoxColumn style = new DataGridTextBoxColumn();
style.MappingName = "dcode";
style.HeaderText = "DCODE";
style.Width = 100;
myGridStyle.GridColumnStyles.Add(style);

DataGridTextBoxColumn style1 = new DataGridTextBoxColumn();
style1.MappingName = "dname";
style1.HeaderText = "DName";
style1.Width = 100;

myGridStyle.GridColumnStyles.Add(style1);
grid.TableStyles.Add(myGridStyle);

}
private void button2_Click(object sender, System.EventArgs e)
{
if(dt.Columns.Count > 1)
{
dt.Columns.RemoveAt(1);
grid.TableStyles[0].GridColumnStyles.RemoveAt(1);
}
button2.Enabled = false;
}
}
}
Nov 16 '05 #3
the reason is sort.

dt.DefaultView.Sort = "";
dt.Columns.RemoveAt(1);

"clyyy2002" <cl*******@yahoo.com.cn> дÈëÏûÏ¢
news:uj*************@TK2MSFTNGP11.phx.gbl...
At added two column and more column hereafter,I immediately click del
button,the program is no problem.

but if i first click the DataGrid columnHeader and then click the del
button, i find when column is

finally ,a error is happen

underside is my program.



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace deldatagridcolumn

{
public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;

private System.Windows.Forms.DataGrid grid;

DataTable dt = new DataTable("T");

public Form1()

{
InitializeComponent();
}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë
private void InitializeComponent()

{

this.grid = new System.Windows.Forms.DataGrid();

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.g rid)).BeginInit();

this.SuspendLayout();

//

// grid

//

this.grid.DataMember = "";

this.grid.HeaderForeColor =
System.Drawing.SystemColors.ControlText;

this.grid.Location = new System.Drawing.Point(24, 16);

this.grid.Name = "grid";

this.grid.Size = new System.Drawing.Size(448, 288);

this.grid.TabIndex = 0;

//

// button1

//

this.button1.Location = new System.Drawing.Point(304, 312);

this.button1.Name = "button1";

this.button1.TabIndex = 1;

this.button1.Text = "Add";

this.button1.Click += new
System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(400, 312);

this.button2.Name = "button2";

this.button2.TabIndex = 2;

this.button2.Text = "Del";

this.button2.Click += new
System.EventHandler(this.button2_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(504, 341);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Controls.Add(this.grid);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.g rid)).EndInit();

this.ResumeLayout(false);

}

#endregion


[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

DataColumn dc = new DataColumn("dcode",typeof(string));

dt.Columns.Add(dc);

DataRow dr = dt.NewRow();

dr[0] = "aa";

dt.Rows.Add(dr);

grid.DataSource = dt;

DataGridTableStyle myGridStyle = new DataGridTableStyle();

myGridStyle.MappingName = "T";

DataGridTextBoxColumn style = new DataGridTextBoxColumn();

style.MappingName = "dcode";

style.HeaderText = "DCODE";

style.Width = 100;

myGridStyle.GridColumnStyles.Add(style);

grid.TableStyles.Add(myGridStyle);

}

private void button1_Click(object sender, System.EventArgs e)

{
string FieldEn = "";

int i=0;

int j=0;

string caption = "";

while(FieldEn.Length==0)

{


for(j=0;j<dt.Columns.Count;j++)

{

if(("F" + i.ToString()) == dt.Columns[j].ColumnName)
{

break;

}

}


if(j==dt.Columns.Count)

{

FieldEn = "F" + i.ToString();

caption = "f" + i.ToString();

break;

}

i++;

}

DataColumn column = new DataColumn(FieldEn,typeof(string));

column.Caption = caption;

dt.Columns.Add(column);

DataGridTextBoxColumn myGridStyle = new
DataGridTextBoxColumn();

myGridStyle.MappingName = column.ColumnName;

myGridStyle.HeaderText = column.Caption;

myGridStyle.Width = 100;

grid.TableStyles[0].GridColumnStyles.Add(myGridStyle);

}

private void button2_Click(object sender, System.EventArgs e)

{

if(dt.Columns.Count > 1)

{

dt.Columns.RemoveAt(1);

grid.TableStyles[0].GridColumnStyles.RemoveAt(1);

}

}

}

}


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Emerson | last post by:
The following assumes a System.Windows.Forms.DataGrid with a System.Data.DataTable set as the DataSource. I'm programming in C# Here's my scenario I click in a cell on a DataGrid. I enter some...
3
by: Newbie | last post by:
I am using a Datagrid to show the contents of a DataTable. But it seems like the Datagrid is not getting the contents of the Datatable when the button ( btnAddAnotherLaborCategory) is clicked. ...
4
by: Glenn Owens | last post by:
I have a DataGrid web control which I've dynamically populated with template columns to be used for bulk-editting. Generally, all of the columns are textbox and/or dropdownlist child controls. ...
8
by: Inigo Jimenez | last post by:
I have an ASP .net web application installed in a Windows 2003 server. This web application has a webform that has a Datagrid. This Datagrid is filled with the data of a SQL table. I have a...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
4
by: The Alchemist | last post by:
I am having a problem with a dynamically-generated Datagrid. It is important to point out that this problem does not exist with a design-time created Datagrid, but only with a dynamically generated...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
4
by: Jan Nielsen | last post by:
Hi all I'm a former Access developer who would like to implement a many-to-many relation in about the same way you do in Access: With a subform and a combo box. Is it possible to use a...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.