While editing a cell that is newly created in GridView throws Index out of range error
-
namespace WindowsFormsApplication1 { public partial class Form1 : Form { private List<Data> dataList = new List<Data>(); public Form1() { InitializeComponent(); // FillData(); dataGridView1.DataSource = dataList; } private void addTToolStripMenuItem_Click(object sender, EventArgs e) { dataList.Add(new Data() { Name = "V", LastName = "J" }); dataGridView1.DataSource = null; dataGridView1.DataSource = dataList; } private void FillData() { dataList.Add(new Data() { Name = "V", LastName = "J" }); dataList.Add(new Data() { Name = "A", LastName = "B" }); dataList.Add(new Data() { Name = "C", LastName = "D" }); } } public class Data { public string Name { get; set; } public string LastName { get; set; } } }
In the above code I try to add a data in the List through gridview by using context menu. Its working fine if already List is populated by data. But fails if I try to add a data without the List is populated. Scenario on fail: screen 1: Form with empty gridview is shown. screen 2: I right click the gridview which show the context menu "Add", when I clicks addTToolStripMenuItem_Click method is called. which creates the data and adds in the list. screen 3: The gridview with the one data is appearing. scrren 4: When I tried to click on the cell ( i.e, newly created ). The error is getting thrown "IndexOutOfRange" -1. Is I am missing any propeties to set. Thanks in advanceRegards, Vythees Miles to go before sleep...
-
namespace WindowsFormsApplication1 { public partial class Form1 : Form { private List<Data> dataList = new List<Data>(); public Form1() { InitializeComponent(); // FillData(); dataGridView1.DataSource = dataList; } private void addTToolStripMenuItem_Click(object sender, EventArgs e) { dataList.Add(new Data() { Name = "V", LastName = "J" }); dataGridView1.DataSource = null; dataGridView1.DataSource = dataList; } private void FillData() { dataList.Add(new Data() { Name = "V", LastName = "J" }); dataList.Add(new Data() { Name = "A", LastName = "B" }); dataList.Add(new Data() { Name = "C", LastName = "D" }); } } public class Data { public string Name { get; set; } public string LastName { get; set; } } }
In the above code I try to add a data in the List through gridview by using context menu. Its working fine if already List is populated by data. But fails if I try to add a data without the List is populated. Scenario on fail: screen 1: Form with empty gridview is shown. screen 2: I right click the gridview which show the context menu "Add", when I clicks addTToolStripMenuItem_Click method is called. which creates the data and adds in the list. screen 3: The gridview with the one data is appearing. scrren 4: When I tried to click on the cell ( i.e, newly created ). The error is getting thrown "IndexOutOfRange" -1. Is I am missing any propeties to set. Thanks in advanceRegards, Vythees Miles to go before sleep...
vytheese wrote:
dataList.Add(new Data() { Name = "V", LastName = "J" }); dataGridView1.DataSource = null;
Hi, I haven't used this, but it seems to me the order of the above lines is wrong; I would remove the bind, modify, then reinstate the bind. :)
Luc Pattyn [Forum Guidelines] [My Articles]
I dislike the black-and-white voting system on questions/answers. X|
-
vytheese wrote:
dataList.Add(new Data() { Name = "V", LastName = "J" }); dataGridView1.DataSource = null;
Hi, I haven't used this, but it seems to me the order of the above lines is wrong; I would remove the bind, modify, then reinstate the bind. :)
Luc Pattyn [Forum Guidelines] [My Articles]
I dislike the black-and-white voting system on questions/answers. X|