每当添加新记录时,添加删除linklabel列

时间:2014-05-12 08:24:40

标签: c# winforms

在我的Windows窗体应用程序中,我想针对每个数据行添加Delete 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列。

亲切地建议我,等你回复。 感谢。

1 个答案:

答案 0 :(得分:0)

试试这个

将“dgvLink”更改为“lnk_delete”。 “lnk_delete”是列名。

 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("lnk_delete");
  if (check == false)
  {
    dataGridView1.Columns.Add(dgvLink);
  }