在我的Windows应用程序中,我想为datagridview中的每条记录添加linklabel,即Delete
,我正在从数据库填充datagridview,如下所示:
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=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\Administrator\\Documents\\Contact.mdf;Integrated Security=True;");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
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];
ds.Tables[0].Columns.Add("Delete", typeof(String));
foreach (DataGridViewRow r in dataGridView1.Rows)
{
DataGridViewLinkCell lc = new DataGridViewLinkCell();
lc.Value = r.Cells[2].Value = "Delete";
dataGridView1[2, r.Index] = lc;
}
clear();
}
public void clear()
{
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
}
private void Form1_Load(object sender, EventArgs e)
{
bindDatagridview();
}
}
}
上面的代码为行中的每个数据添加Delete
linkLabel,但是当我点击datagridview的headertext时,linklabel
转换为简单文本,它是怎么回事,并且在那里以任何方式为行中的每个数据添加Delete
linklabel。
请建议我,等待你的回复。
感谢。
答案 0 :(得分:1)
我认为你的问题是专栏的类型!
var col = new DataGridViewLinkColumn();
col.DataPropertyName = "Delete";
col.Name = "Delete";
ds.Tables[0].Columns.Add(col);
应该这样做。
答案 1 :(得分:0)
我正在使用它,它的工作正常。
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;
dataGridView1.Columns.Add(dgvLink);
答案 2 :(得分:0)
单击列标题以对列进行排序,然后查看下划线是否留在列(dgvLink
中)。
当您尝试对列进行排序时,此下划线消失。