0
在我的Windows窗體應用程序,我想補充Delete
LinkLabel的針對每個數據行,我下面做的LinkLabel柱:添加刪除每當新的記錄將被添加
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace search
{
public partial class Form1 : Form
{
SqlConnection connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\Musewerx\\My Documents\\Contacts.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
bindDatagridview();
}
public void bindDatagridview()
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = new SqlCommand("Select * from contactsinfo", connection);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
clear();
DataGridViewLinkColumn dgvLink = new DataGridViewLinkColumn();
dgvLink.UseColumnTextForLinkValue = true;
dgvLink.LinkBehavior = LinkBehavior.SystemDefault;
dgvLink.HeaderText = "";
dgvLink.Name = "lnk_delete";
dgvLink.LinkColor = Color.Blue;
dgvLink.TrackVisitedState = true;
dgvLink.Text = "Delete";
dgvLink.UseColumnTextForLinkValue = true;
bool check = dataGridView1.Columns.Contains("dgvLink");
if(check == false)
{
dataGridView1.Columns.Add(dgvLink);
}
}
public void clear()
{
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty)
{
MessageBox.Show("Enter Contact Name");
}
else if(textBox2.Text == string.Empty)
{
MessageBox.Show("Enter Contact Number");
}
else
{
da.InsertCommand = new SqlCommand("Insert into contactsinfo(ContactName,ContactNumber) Values('" + textBox1.Text + "','" + textBox2.Text + "')", connection);
connection.Open();
da.InsertCommand.ExecuteNonQuery();
bindDatagridview();
clear();
connection.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
但問題是,每一次當我輸入新記錄新列Delete
linklabel將被添加,並且我只想要一次添加一個Delete
linklabel列。
好心建議我,等待您的回覆。 謝謝。
只是把一個if條件來檢查列已經存在。如果不存在,則添加。否則不要添加。 – ray
@ray:根據您的建議,我編輯了我的帖子,但仍然無法正常工作。 – user88