Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,473 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,550 people online right now. Registration is fast and FREE... Join Now!




datagridview

 
Reply to this topicStart new topic

datagridview, how to delete rows from grid?

mamta takarkhede
30 Jun, 2008 - 11:13 AM
Post #1

New D.I.C Head
*

Joined: 30 Jun, 2008
Posts: 2

hi

how to delete rows from grid?
& also how to display selected cells on labels?

when I select perticular row to delete then row is deleted from database but in gride view all rows are disappeared even through data is still there in database.

private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(" Data Source=VINOD-AD973EE47\\SQLEXPRESS;Initial Catalog=FIS;Integrated Security=True");
con.Open();
string sql = "Select * From ProductBOM ";
SqlCommand cmd = new SqlCommand(sql, con);
childID = Convert.ToInt32(gridBOM.SelectedCells[0].OwningRow.Cells[0].Value.ToString());
cmd.CommandText = "Delete From ProductBOM Where Id="+ childID +"";
//DataSet dsdata = new DataSet();
con.Close();
SqlDataAdapter mydataAdapter = new SqlDataAdapter(cmd);
mydataAdapter.Fill(dataset, "ProductBOM");
gridBOM.DataSource = dataset.Tables[0].DefaultView;
MessageBox.Show("Item Deleted");

try
{
childID = GetChildId();
if (childID > 0)
{

DataColumn[] dc = new DataColumn[1];
dc[0] =dataset.Tables[0].Columns[0];
dataset.Tables[0].PrimaryKey = dc;
DataRow dr =dataset.Tables[0].Rows.Find(childID.ToString());
if (dr != null)
{
dataset.Tables[0].Rows.Remove(dr);
}
}
}
catch (SqlException sexception)
{
if (sexception.Message.Equals("Unitused"))
{
Utility.ShowErrorMessage("Unitused");
}
else
{
Utility.ShowExceptionMessage(sexception.Message);
}
}
catch (Exception ex)
{
Utility.ShowExceptionMessage(ex.Message);
}
}

This post has been edited by mamta takarkhede: 30 Jun, 2008 - 11:30 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 05:55PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month