Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. While editing a cell that is newly created in GridView throws Index out of range error

While editing a cell that is newly created in GridView throws Index out of range error

Scheduled Pinned Locked Moved C#
databasehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vytheese
    wrote on last edited by
    #1

    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 advance

    Regards, Vythees Miles to go before sleep...

    L 1 Reply Last reply
    0
    • V vytheese

      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 advance

      Regards, Vythees Miles to go before sleep...

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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|


      V 1 Reply Last reply
      0
      • L Luc Pattyn

        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|


        V Offline
        V Offline
        vytheese
        wrote on last edited by
        #3

        Like this, dataGridView1.DataSource = null; dataList.Add(new Data() { Name = "V", LastName = "J" }); dataGridView1.DataSource = dataList; I tried that too, Not working.. :sigh:

        Regards, Vythees Miles to go before sleep...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups