Hi All, I have a datagridview which is for display the objects. the datagridview is not connected to database directly but binding to the list/collection/enumerator, When I try to delete the selected row from the datagridview, it shows me error "Rows cannot be programmatically removed unless the datagridview is data-bound to an IBindingList that support change notification and allows deletion."
Dont really understand this, please kindly give me some suggestions. Thanks in advance, regards, CC Code as attached.
private void BindCarousels()
{ CarouselCollection allCarousels = (new CarouselBLL()).GetAllCarousels();
dgvAllCarousels.DataSource = allCarousels;
}
private void btnDeleteCarousel_Click(object sender, EventArgs e)
{
int carousel_id =Convert.ToInt32(dgvAllCarousels.CurrentRow.Cells["Carousel_Id"].Value);
(new CarouselBLL()).DeleteCarousel(carousel_id);
dgvAllCarousels.Rows.RemoveAt(dgvAllCarousels.CurrentRow.Index);
}
- Moved byHarry ZhuMSFTTuesday, September 22, 2009 8:58 AMrelating to datagridview (From:Visual C# General)
- Changed TypeRoyal Oak Friday, September 25, 2009 10:36 PMproblem unsolve
- Changed TypeAland LiMSFT, ModeratorFriday, September 25, 2009 7:31 AMNo reply
-
| | Royal Oak Saturday, September 19, 2009 12:48 PM | Hi John, I've changed the function to retrieve data from the database in datalayer, now I am able to delete the row in datagridview. Code:
public DataTable AllCarouselsTable()
{
SqlConnection allCarouselsConn=new SqlConnection (ConfigurationManager.ConnectionStrings["VirtualCarouselDB"].ConnectionString);
SqlCommand allCarouselsComm = new SqlCommand("select * from carousel",allCarouselsConn);
allCarouselsConn.Open();
allCarouselsComm.ExecuteNonQuery();
DataTable table = new DataTable();
SqlDataReader dr = allCarouselsComm.ExecuteReader();
table.Columns.Add(new DataColumn("carousel_id",typeof(System.Int32)));
table.Columns.Add(new DataColumn("carousel_name", typeof(System.String)));
table.Columns.Add(new DataColumn("carousel_description", typeof(System.String)));
table.Columns.Add(new DataColumn("carousel_created_by", typeof(System.String)));
table.Columns.Add(new DataColumn("carousel_objects", typeof(System.Int32)));
while(dr.Read())
{
DataRow datarow= table.NewRow();
datarow[0]=dr.GetInt32(0);
datarow[1]=dr.GetString(1);
datarow[2]=dr.GetString(2);
datarow[3] = dr.GetString(3);
datarow[4] = dr.GetInt32(4);
table.Rows.Add(datarow);
}
allCarouselsConn.Close();
return table;
}
This is an alternative solution for my applicaiton, however, I m still concerned about the list/collection/enumerator way to get data from the database. As there will be lots of changing in my code, would really appreciate it if you guys can come up with any other solutions. Thank you, Cris - Marked As Answer byRoyal Oak Sunday, September 27, 2009 9:47 PM
-
| | Royal Oak Saturday, September 26, 2009 12:20 AM | (dgvAllCarousels.DataSource as DataTable).Rows.RemoveAt(.......); John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove Saturday, September 19, 2009 3:04 PM | Hi John, It doesnt work for me.
private void btnDeleteCarousel_Click(object sender, EventArgs e)
{
int carousel_id =Convert.ToInt32(dgvAllCarousels.CurrentRow.Cells["Carousel_Id"].Value);
(dgvAllCarousels.DataSource as DataTable).Rows.RemoveAt(dgvAllCarousels.CurrentRow.Index);
(new CarouselBLL()).DeleteCarousel(carousel_id);
//dgvAllCarousels.Rows.RemoveAt(dgvAllCarousels.CurrentRow.Index);
}
it shows the nullreferenceexception error "Object reference not set to an instance of an object." More helps needed, Cheers CC | | Royal Oak Saturday, September 19, 2009 11:02 PM | Paste all of your code.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove Sunday, September 20, 2009 3:12 AM | Hi xiaoca324,
Could you please let us know the type of the data source which is bound to the DataGridView?
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. | | Aland Li Wednesday, September 23, 2009 5:42 AM | Hi,
We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. | | Aland Li Friday, September 25, 2009 7:31 AM | Hi John & Aland, Sorry about the late reply. I was away for few days so that I didnt follow up the post. My apologies to you guys. The data source in my application: the datagridview is not connected to database directly but binding to the list/collection/enumerator. The code are as below:
public CarouselCollection GetAllCarousels()
{
CarouselCollection allCarousels = new CarouselCollection();
Carousel carousel;
using (SqlConnection carouselConn = new SqlConnection(ConfigurationManager.ConnectionStrings["VirtualCarouselDB"].ConnectionString))
{
carouselConn.Open();
using (SqlCommand sqlCommand = new SqlCommand("select * from carousel", carouselConn))
{
using (SqlDataReader dr = sqlCommand.ExecuteReader())
{
while (dr.Read())
{
carousel = new Carousel();
carousel.Carousel_Id = Convert.ToInt32(dr["Carousel_Id"]);
carousel.Carousel_Name = Convert.ToString(dr["Carousel_Name"]);
carousel.Carousel_Description = Convert.ToString(dr["Carousel_Description"]);
carousel.Carousel_Created_By = Convert.ToString(dr["Carousel_Created_By"]);
carousel.Carousel_Objects = Convert.ToInt32(dr["Carousel_Objects"]);
allCarousels.Add(carousel);
}
}
}
carouselConn.Close();
}
return allCarousels;
}
The code that I use to bind to datagridview:
private void BindCarousels()
{ CarouselCollection allCarousels = (new CarouselBLL()).GetAllCarousels();
dgvAllCarousels.DataSource = allCarousels;
}
I have no problem binding the records to datagridview, however when I try to delete a row from datagridview using the John's code as below:
private void btnDeleteCarousel_Click(object sender, EventArgs e)
{
int carousel_id =Convert.ToInt32(dgvAllCarousels.CurrentRow.Cells["Carousel_Id"].Value);
(dgvAllCarousels.DataSource as DataTable).Rows.RemoveAt(dgvAllCarousels.CurrentRow.Index);
(new CarouselBLL()).DeleteCarousel(carousel_id);
}
The outcome is the record can be deleted from the database, however, it give the error message: "Object reference not set to an instance of an object." What causes this? Hope I give enough information to you guys, if not, please feel free to ask, Thanks in advance, Cris | | Royal Oak Friday, September 25, 2009 10:33 PM | Don't use RemoveAt, that removes the row from the DataTable and does not mark it for deletion. Use the Row.Delete function instead.
(dgvAllCarousels.DataSource as DataTable).Rows[dgvAllCarousels.CurrentRow.Index].Delete();
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove Friday, September 25, 2009 10:37 PM | Hi John, Thank you for reply so soon. Again, I had no luck this time by using delete. The same error message shown: "Object reference not set to an instance of an object." if (Convert.ToInt32(dgvAllCarousels.CurrentRow.Cells["carousel_objects"].Value) == 0) { int carousel_id = Convert.ToInt32(dgvAllCarousels.CurrentRow.Cells["Carousel_Id"].Value); (new CarouselBLL()).DeleteCarousel(carousel_id); (dgvAllCarousels.DataSource as DataTable).Rows[dgvAllCarousels.CurrentRow.Index].Delete(); //(dgvAllCarousels.DataSource as DataTable).Rows.RemoveAt(dgvAllCarousels.CurrentRow.Index); //dgvAllCarousels.Rows.RemoveAt(dgvAllCarousels.CurrentRow.Index); } Here is the code for (new CarouselBLL()).DeleteCarousel(carousel_id);
public bool DeleteCarousel(int carousel_Id)
{
try
{
using (SqlConnection carouselConn = new SqlConnection(ConfigurationManager.ConnectionStrings["VirtualCarouselDB"].ConnectionString))
{
carouselConn.Open();
using (SqlCommand carouselComm = new SqlCommand("delete from carousel where carousel_id= " + carousel_Id.ToString(), carouselConn))
{
carouselComm.ExecuteNonQuery();
}
}
return true;
}
catch
{
return false;
}
}
Cheers, | | Royal Oak Friday, September 25, 2009 10:48 PM | Hi John, I've changed the function to retrieve data from the database in datalayer, now I am able to delete the row in datagridview. Code:
public DataTable AllCarouselsTable()
{
SqlConnection allCarouselsConn=new SqlConnection (ConfigurationManager.ConnectionStrings["VirtualCarouselDB"].ConnectionString);
SqlCommand allCarouselsComm = new SqlCommand("select * from carousel",allCarouselsConn);
allCarouselsConn.Open();
allCarouselsComm.ExecuteNonQuery();
DataTable table = new DataTable();
SqlDataReader dr = allCarouselsComm.ExecuteReader();
table.Columns.Add(new DataColumn("carousel_id",typeof(System.Int32)));
table.Columns.Add(new DataColumn("carousel_name", typeof(System.String)));
table.Columns.Add(new DataColumn("carousel_description", typeof(System.String)));
table.Columns.Add(new DataColumn("carousel_created_by", typeof(System.String)));
table.Columns.Add(new DataColumn("carousel_objects", typeof(System.Int32)));
while(dr.Read())
{
DataRow datarow= table.NewRow();
datarow[0]=dr.GetInt32(0);
datarow[1]=dr.GetString(1);
datarow[2]=dr.GetString(2);
datarow[3] = dr.GetString(3);
datarow[4] = dr.GetInt32(4);
table.Rows.Add(datarow);
}
allCarouselsConn.Close();
return table;
}
This is an alternative solution for my applicaiton, however, I m still concerned about the list/collection/enumerator way to get data from the database. As there will be lots of changing in my code, would really appreciate it if you guys can come up with any other solutions. Thank you, Cris - Marked As Answer byRoyal Oak Sunday, September 27, 2009 9:47 PM
-
| | Royal Oak Saturday, September 26, 2009 12:20 AM |
|