views:

114

answers:

1

I'm trying to create a custom DataGridViewButtonCell to download a file once clicked. That class works flawlessly, but will NOT add itself to the DataGridView when I use the rows.add() method. It seems to just use the ToString() method for a label then create its own from the CellTemplate.

The type for the particular column is DataGridViewDownloadColumn. I've also run tests outputting the values held in the grid view and it IS the right class being instantiated.

Here's my code without the layout info:

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();
        }
    }
}

I've been fighting this crap for hours. Any help is appreciated.

A: 

It seems that for some reason, datagridview.rows.add(object list) doesn't cut it if you make custom objects with independant constructors. What you need to do, is create a new row, add cells individually, then throw that row into the list.

        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);
        }
RandomInsano