2014-05-09 2 views
1

, 내가있는 DataGridView에서 각 레코드에 대해에 LinkLabel 즉 Delete을 추가 할 윈도우 폼 응용 프로그램의 C#에서 행의 데이터에 대한 링크 라벨을 삭제 추가하고 내가 좋아하는 아래의 데이터베이스에서 DataGridView를 채우기 오전 :내 Windows 응용 프로그램에서

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을 추가 할 수있는 방법이 방법, 간단한 텍스트로 변환 행의 데이터. 친절하게 답장을 기다리고 있습니다. 감사합니다. .

답변

1

문제가 열의 유형이라고 생각합니다.

var col = new DataGridViewLinkColumn(); 
col.DataPropertyName = "Delete"; 
col.Name = "Delete"; 

ds.Tables[0].Columns.Add(col); 

해야합니다.

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); 
+0

제가 말한 것처럼 ^^ – DJmRek