error datagrid new line

S

Seok-Kyu Kong

Hi. All.
I make a datagrid on form, and create dataset, datatable and binding them.
I make a button that creates new line in datagrid.
OK... it's successful to make new line but when i select 2nd column,
error message occurs as follows...

Help me!!!

ERROR MESSAGE
=================
System.IndexOutOfRangeException throws... -_-;


Some extract of my sources...
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace GridSample
{
/// <summary>
/// Form1
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid grid;
private System.Windows.Forms.Button btnMakeData;
private System.Windows.Forms.MainMenu mainMenu1;

private DataSet myDataSet;
private System.Windows.Forms.Button btnNewLine;
private DataTable myDataTable;

public Form1()
{
//
//
InitializeComponent();

//
//
}
/// <summary>
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.grid = new System.Windows.Forms.DataGrid();
this.btnMakeData = new System.Windows.Forms.Button();
this.btnNewLine = new System.Windows.Forms.Button();
//
// grid
//
this.grid.Location = new System.Drawing.Point(8, 8);
this.grid.Size = new System.Drawing.Size(224, 144);
this.grid.Text = "dataGrid1";
//
// btnMakeData
//
this.btnMakeData.Location = new System.Drawing.Point(8, 168);
this.btnMakeData.Size = new System.Drawing.Size(104, 24);
this.btnMakeData.Text = "ÀÚ·á»ý¼º";
this.btnMakeData.Click += new
System.EventHandler(this.btnMakeData_Click);
//
// btnNewLine
//
this.btnNewLine.Location = new System.Drawing.Point(144, 168);
this.btnNewLine.Size = new System.Drawing.Size(88, 24);
this.btnNewLine.Text = "»õ Çà Ãß°¡";
this.btnNewLine.Click += new System.EventHandler(this.btnNewLine_Click);
//
// Form1
//
this.Controls.Add(this.btnNewLine);
this.Controls.Add(this.btnMakeData);
this.Controls.Add(this.grid);
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Text = "Form1";

}
#endregion

/// <summary>
/// </summary>

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

private void MakeDataSet()
{
myDataSet = new DataSet("myDataSet");

DataTable tCust = new DataTable("Customers");

DataColumn cCustID = new DataColumn("CustID", typeof(int));
DataColumn cCustName = new DataColumn("CustName");
DataColumn cCurrent = new DataColumn("Current", typeof(bool));
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCustName);
tCust.Columns.Add(cCurrent);

myDataSet.Tables.Add(tCust);

DataRow newRow1;
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = i;
tCust.Rows.Add(newRow1);
}
tCust.Rows[0]["custName"] = "Customer1";
tCust.Rows[1]["custName"] = "Customer2";
tCust.Rows[2]["custName"] = "Customer3";

tCust.Rows[0]["Current"] = true;
tCust.Rows[1]["Current"] = true;
tCust.Rows[2]["Current"] = false;
}

private void btnMakeData_Click(object sender, System.EventArgs e)
{
MakeDataSet();

myDataTable = myDataSet.Tables["Customers"];
grid.DataSource = myDataTable;

}

private void btnNewLine_Click(object sender, System.EventArgs e)
{
myDataTable.DefaultView.AddNew();
}
}
}
 
W

William Ryan

Bind to a DataTable as you are, leave out the
DefaultView. That returns an index to the Dataview which
doesn't appear to exist. If you bind a grid to a table,
anything you add to the table will show in the grid.

Good Luck,

Bill
(e-mail address removed)

-----Original Message-----
Hi. All.
I make a datagrid on form, and create dataset, datatable and binding them.
I make a button that creates new line in datagrid.
OK... it's successful to make new line but when i select 2nd column,
error message occurs as follows...

Help me!!!

ERROR MESSAGE
=================
System.IndexOutOfRangeException throws... -_-;


Some extract of my sources...
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace GridSample
{
/// <summary>
/// Form1
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid grid;
private System.Windows.Forms.Button btnMakeData;
private System.Windows.Forms.MainMenu mainMenu1;

private DataSet myDataSet;
private System.Windows.Forms.Button btnNewLine;
private DataTable myDataTable;

public Form1()
{
//
//
InitializeComponent();

//
//
}
/// <summary>
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.grid = new System.Windows.Forms.DataGrid();
this.btnMakeData = new System.Windows.Forms.Button();
this.btnNewLine = new System.Windows.Forms.Button();
//
// grid
//
this.grid.Location = new System.Drawing.Point(8, 8);
this.grid.Size = new System.Drawing.Size(224, 144);
this.grid.Text = "dataGrid1";
//
// btnMakeData
//
this.btnMakeData.Location = new System.Drawing.Point (8, 168);
this.btnMakeData.Size = new System.Drawing.Size(104, 24);
this.btnMakeData.Text = "ÀÚ·á»ý¼º";
this.btnMakeData.Click += new
System.EventHandler(this.btnMakeData_Click);
//
// btnNewLine
//
this.btnNewLine.Location = new System.Drawing.Point (144, 168);
this.btnNewLine.Size = new System.Drawing.Size(88, 24);
this.btnNewLine.Text = "»õ Çà Ãß°¡";
this.btnNewLine.Click += new System.EventHandler (this.btnNewLine_Click);
//
// Form1
//
this.Controls.Add(this.btnNewLine);
this.Controls.Add(this.btnMakeData);
this.Controls.Add(this.grid);
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Text = "Form1";

}
#endregion

/// <summary>
/// </summary>

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

private void MakeDataSet()
{
myDataSet = new DataSet("myDataSet");

DataTable tCust = new DataTable("Customers");

DataColumn cCustID = new DataColumn("CustID", typeof (int));
DataColumn cCustName = new DataColumn("CustName");
DataColumn cCurrent = new DataColumn("Current", typeof (bool));
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCustName);
tCust.Columns.Add(cCurrent);

myDataSet.Tables.Add(tCust);

DataRow newRow1;
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = i;
tCust.Rows.Add(newRow1);
}
tCust.Rows[0]["custName"] = "Customer1";
tCust.Rows[1]["custName"] = "Customer2";
tCust.Rows[2]["custName"] = "Customer3";

tCust.Rows[0]["Current"] = true;
tCust.Rows[1]["Current"] = true;
tCust.Rows[2]["Current"] = false;
}

private void btnMakeData_Click(object sender, System.EventArgs e)
{
MakeDataSet();

myDataTable = myDataSet.Tables["Customers"];
grid.DataSource = myDataTable;

}

private void btnNewLine_Click(object sender, System.EventArgs e)
{
myDataTable.DefaultView.AddNew();
}
}
}


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top