刪除DataGridView選中行並更新資料庫,datagridview資料庫

來源:互聯網
上載者:User

刪除DataGridView選中行並更新資料庫,datagridview資料庫

  前面寫過一篇文章是DataGridView控制項顯示資料的,DataGridView在與資料庫打交道時會經常出現,也很實用。通過DataGridView對資料庫變更和查詢都比較方便。

  這裡我們需要用DataGridView資料,並通過選中行將資料從資料庫中刪除。

  其原理是把選中記錄的主鍵提取出來,然後傳給實體,通過實體給D層傳值實現對資料庫的修改。

  下面是各層代碼,供大家參考。

  介面層代碼都是D層的父類方法,這裡只給出D層代碼:

<span style="background-color: rgb(255, 255, 255);"><span style="font-family:KaiTi_GB2312;font-size:18px;">    '重寫刪除使用者介面方法    Public Function DelUser(user As Entity.LoginEntity) As Integer Implements IAddDel.DelUser        Dim strSQL As String = "delete from User_info where userName=@username"        Dim params() As SqlParameter = {New SqlParameter("@username", user.user_name)}        Dim helper As New SqlHelper        Dim int = helper.ExecuteNoQuery(strSQL, CommandType.Text, params)        Return int    End Function</span></span>

  抽象工廠代碼:

<span style="background-color: rgb(255, 255, 255);"><span style="font-family:KaiTi_GB2312;font-size:18px;">  Private Shared ReadOnly AssemblyName As String = "DAL" '聲明程式集名稱    Private Shared ReadOnly db As String = ConfigurationManager.AppSettings("DB") '讀取設定檔    Public Function AddDel() As IAddDel        Dim className As String = AssemblyName + "." + db + "AddDelDAL"        Dim iadddel As IAddDel        iadddel = CType(Assembly.Load(AssemblyName).CreateInstance(className), IAddDel) '反射        Return iadddel    End Function</span></span>

  B層代碼:

<span style="background-color: rgb(255, 255, 255);"><span style="font-family:KaiTi_GB2312;font-size:18px;">    '判斷是否刪除成功    Public Function IsDelUser(ByVal user As Entity.LoginEntity) As Boolean        Dim int = iadddel.DelUser(user)        If int = 1 Then            Return True        Else            Return False        End If    End Function</span></span>

  U層代碼

<span style="background-color: rgb(255, 255, 255);"><span style="font-family:KaiTi_GB2312;font-size:18px;"> Dim k As Integer = gvwUser.SelectedRows.Count        Dim thisUser As New Entity.LoginEntity        Dim ub As New BLL.AddDelBLL        '判斷是否有選擇記錄        If k > 0 Then            If MessageBox.Show("刪除使用者後將無法恢複!是否繼續刪除?", "提示", MessageBoxButtons.YesNo, _                               MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then                '從下往上刪,避免沙漏效應                  For i As Integer = k To 1 Step -1                    '擷取使用者名稱                    thisUser.user_name = gvwUser.SelectedRows(i - 1).Cells("userName").Value.ToString                    '判斷選中使用者是否為登入使用者                    If thisUser.user_name = UserName Then                        MsgBox("目前使用者不能被刪除,請重新選擇!", vbOKOnly + vbExclamation, "系統提示")                        Exit Sub                    Else                        If ub.IsDelUser(thisUser) = True Then                            MsgBox("刪除成功!", vbOKOnly + vbInformation, "系統提示")                        Else                            MessageBox.Show("刪除失敗!", "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error)                        End If                        '將從資料庫中刪除的資訊從Datagridview1中刪除                          gvwUser.Rows.RemoveAt(gvwUser.SelectedRows(i - 1).Index)                    End If                Next            End If        Else            MsgBox("請選中要刪除的行")            Exit Sub        End If    End Sub</span></span>

  效果如下:

  刪除前:                                             刪除後:






  我們通過DataGridView對資料進行操作更加直觀,但是資料無價,操作資料庫時一定要謹慎!以免給我們帶來不必要的麻煩。必要時刪除前要給與提示,是否確定刪除,或者提高操作許可權,方便操作的前提是保證資料的安全性!


C# datagridview刪除選中的行並且資料庫裡也更新,代碼是什?

你在網上找的代碼也沒找全。。
給個簡單的例子你吧
首先你要選擇一行進行刪除,綁定的時候表的ID肯定要存在表中,或者用tag等等儲存,我這就已第一列是ID為例,還有要選擇一行進行刪除,綁定的時候dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;這句話少不了,意思是只允許選擇一行。
然後在button事件裡
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("你的資料庫連接字串");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是ID
string delete_by_id = "delete from ?? where 表的ID=" + select_id;//sql刪除語句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.EndExecuteNonQuery();
MessageBox.Show("刪除成功!");
}
catch
{
MessageBox.Show("請正確選擇行!");
}
finally
{
con.Dispose();
}
}
 
DataGridView中刪除選中的整行資料,但要同時刪除資料庫裡的資料,要怎寫? 我用的是sql

DataGridView是有綁定 dataset 或者datatable 的,你要把資料來源裡面的那一行刪除,然後重新綁定或者重新整理下DataGridView 就可以了
同時還要寫語句刪除資料庫~
如:
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(sql_connect);
cmd.CommandText = "delete from gl_card_no where card_no = '" + textBox3.Text.Trim() + "'";
if (MessageBox.Show("你確定刪除該行嗎?" + cmd.CommandText + "", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{

try
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
MessageBox.Show("刪除成功!");
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
finally
{
cmd.Dispose();
}
}
 

相關關鍵詞:
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.