Solved

Deleting from a DataGridView control

Posted on 2009-05-08
4
489 Views
Last Modified: 2012-05-06
Im extremly new to C# and i have a datagridview polpulated already but would like to know how i can delete a row in SQL using the datagridcontrol. I can delete a row from the datagridview but i want to apply that to SQL server db table too. Can anyone help?
I can specify the connection, DataAdaptor etc but im stuck after that. I would need to create A SQL DELETE command and update at the end but thats all i know
private void btnDelete_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection();
            conn = new SqlConnection(@"Data Source=<SQLInstance>;Initial Catalog=<Database>;Integrated Security=True");
            da = new SqlDataAdapter();
            ds = new DataSet();
            datagridview1.Rows.RemoveAt(datagridview1.CurrentRow.Index);  //applys delete to the gid but i want to delete the record in SQL too
 
        }

Open in new window

0
Comment
Question by:wilko100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:ddayx10
ID: 24341633
HI,

I don't often use dataadapters as I use the disconnected model more often. I'm 99% sure thought that all you need do is assign the sqldataadapter.deletecommand, sqldataadapter.delete and finally rebind.

A short example:
da.DeleteCommand = "DELETE FROM tbl WHERE ColumnId =" + [GetValueFromDataGridView HERE]
da.Delete

Now fire through your original binding process, or since you've already removed the row you may not need to.

A more full scale example can be found at this link:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.deletecommand(VS.71).aspx
0
 

Accepted Solution

by:
wilko100 earned 0 total points
ID: 24342606
Yeah that makes sense, you know wht the syntax is the sql statement line? i know it would be something like "DELETE FROM Contact WHERE FirstName = "+{CurrentRowSelect}+" but you know what the bit in {} is? then is that all i need?

This is the code i have so far...
private void btnDelete_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection();
            conn = new SqlConnection(@"Data Source=<SQLInstanne>;Initial Catalog=<Database>;Integrated Security=True");
            comm = new SqlCommand("DELETE FROM Contact WHERE "+RowOfDataGrid+"");            
            da = new SqlDataAdapter();
            ds = new DataSet();
            comm.Connection = conn;
            da.DeleteCommand = comm;
            da.Fill(ds);
            conn.Open();
            datagridview1.DataSource = ds.Tables[0];
            var UserTable = (DataTable)datagridview1.DataSource;
            datagridview1.Rows.RemoveAt(datagridview1.CurrentRow.Index);
            
                    
            
        }

Open in new window

0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24342658
Well in SQL Delete you are going to delete based on a unique value of the row in question. Usually this is the primary key column.

I cant tell you exactly without building one myself, but I suspect it is something like this:

datagridview1.Rows(datagridview1.CurrentRow.Index).cells(index of your value).value;

Funny thing I wasnt aware a gridview had a property for "currentrow". It's not listed on MSDN as having that property, but you said you had it working already using that "removeat" code so Im going with it.

Dday
0
 

Author Comment

by:wilko100
ID: 24343632
Yep the currentrow works a treat and really easy when its disconnected, im clueelss when i want to delete from within the backend! I used...(See code below) which removed the row selected, i just dont know what the syntax is for deleting the selected row

Would you guys say this is the order to delete?:
1) Create connection
2) Create DELETE Command (DELETE Counter, FirstName, LastName etc FROM [Table] WHERE Counter = "+Selected Row on Grid+");
3) Create DataAdaptor
4) Create Dataset
5) Update


private void btnDelete_Click(object sender, EventArgs e)
        {             
            
            
            [GridControlName].Rows.RemoveAt([GridControlName].CurrentRow.Index);           
                    
            
        }

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Amazon S3 .Net error 5 78
Why use this lambda? 12 95
Pdf not loading 2 40
Why am I losing my session variable value 11 39
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question