开发者

Data table in win form

I want开发者_JAVA百科 to copy two columns from excel and work on them in a c# program. What component is the best to use to mimic an excel column in a win form?


You can use the DataGridView control.


You could write a routine that pulls in clipboard data with something like:

    private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // create dataset to hold csv data:
        DataSet ds = new DataSet();
        ds.Tables.Add();
        ds.Tables[0].Columns.Add();
        ds.Tables[0].Columns.Add();
        ds.Tables[0].Columns.Add();
        ds.Tables[0].Columns.Add();
        ds.Tables[0].Columns.Add();
        ds.Tables[0].Columns.Add();

        string[] row = new string[1];

        // read csv data from clipboard
        IDataObject t = Clipboard.GetDataObject();
        System.IO.StreamReader sr =
                new System.IO.StreamReader((System.IO.MemoryStream)t.GetData("csv"));

        // assign csv data to dataset      
        while (!(sr.Peek() == -1))
        {
            row[0] = sr.ReadLine();
            ds.Tables[0].Rows.Add(row);
        }

        // set data source
        dataGridView1.DataSource = ds.Tables[0];
    }

Cleaned up code and tested code sample. It is still rough but demonstrates the capability with a fixed number of columns. (Pasting in more than six will crash, any less should work.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜