2009-12-12 2 views
0

사용자 지정 DataGridViewButtonCell을 만들어 클릭 한 번 파일을 다운로드하려고합니다. 이 클래스는 완벽하게 작동하지만 rows.add() 메서드를 사용할 때 DataGridView에 추가되지 않습니다. 레이블에 ToString() 메서드를 사용하고 CellTemplate에서 고유 한 메서드를 만드는 것처럼 보입니다.C# DataGridView 추가 ToString()을 사용합니까?

특정 열의 형식은 DataGridViewDownloadColumn입니다. 또한 격자보기에있는 값을 출력하는 테스트를 실행했으며 올바른 클래스가 인스턴스화되었습니다.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml; 
using MS.Internal.Xml; 
using System.Net; 

namespace RSS_Catcher 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      XmlReader objReader = XmlReader.Create("http://revision3.com/filmriot/feed/Xvid-Large"); 

      while (objReader.ReadToFollowing("enclosure")) 
      { 
       objReader.MoveToFirstAttribute(); 

       Uri objURI = new Uri(objReader.ReadContentAsString()); 
       string[] objString = objURI.Segments; 

       DownloadButton objDL = new DownloadButton(objURI, objString[objString.Length - 1]); 

       this.dataGridView1.Rows.Add(false, "Hello!", objDL); 
      } 
     } 
    } 

    public class DataGridViewDownloadColumn : DataGridViewButtonColumn 
    { 
     public DataGridViewDownloadColumn() 
     { 
      CellTemplate = new DownloadButton(); 
     } 
    } 

    class DownloadButton : DataGridViewButtonCell 
    { 
     WebClient objDownloader = new WebClient(); 
     Uri  fileURL; 
     string strSavePath; 

     public DownloadButton() 
     { 

     } 

     public DownloadButton(Uri fileURI, string strFilename) : base() 
     { 
      objDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgChanged); 

      this.fileURL  = fileURI; 
      this.strSavePath = strFilename; 

      this.Value = "Download"; 
     } 

     protected override void OnClick(DataGridViewCellEventArgs e) 
     { 
      // If it's downloading, cancel the download 
      if (objDownloader.IsBusy) 
      { 
       objDownloader.CancelAsync(); 
       this.Value = "Download"; 
      } 
      else 
       objDownloader.DownloadFileAsync(fileURL, strSavePath); 

      base.OnClick(e); 
     } 

     void ProgChanged(object sender, DownloadProgressChangedEventArgs e) 
     { 
      this.Value = e.ProgressPercentage + "%"; 
     } 

     public override string ToString() 
     { 
      return 
       "Download Button:\n" + 
       this.fileURL.ToString(); 
     } 
    } 
} 

나는 시간이 쓰레기 싸우고했습니다

다음은 레이아웃 정보없이 내 코드입니다. 어떤 도움을 주셔서 감사합니다.

답변

0

어떤 이유로 인해 독립 생성자를 사용하여 사용자 지정 개체를 만들면 datagridview.rows.add (개체 목록)가 잘라내는 것처럼 보입니다. 당신이해야 할 일은 새로운 행을 만들고, 셀을 개별적으로 추가 한 다음 그 행을 목록에 던지기입니다.

 while (objReader.ReadToFollowing("enclosure")) 
     { 
      objReader.MoveToFirstAttribute(); 

      Uri objURI = new Uri(objReader.ReadContentAsString()); 
      string[] objString = objURI.Segments; 
      string strFileName = objString[objString.Length - 1]; 

      // No string constructor?! What the crap? 
      DataGridViewTextBoxCell objFileCell = new DataGridViewTextBoxCell(); 
      objFileCell.Value = strFileName; 

      DataGridViewRow objRow = new DataGridViewRow(); 
      objRow.Cells.Add(new DataGridViewCheckBoxCell(false)); 
      objRow.Cells.Add(objFileCell); 
      objRow.Cells.Add(new DownloadButton(objURI, strFileName)); 
      this.dataGridView1.Rows.Add(objRow); 
     }