George Polevoy
George Polevoy

Reputation: 7681

How to create excel-like insertion behavior in Winforms?

How to create a view, which accepts a column of string values, similar to that of Excel. When i copy-paste multiple lines of text, each of them must be inserted in it's own row.

Here is my code, but i get only one cell inserted.

var dt = new DataTable("Guids");
dt.Columns.Add("Guid");
dataGridView1.DataSource = dt;

It does not need to be implemented as a datagridview, if other type of control suites better.

Upvotes: 2

Views: 378

Answers (1)

bronsoja
bronsoja

Reputation: 809

Possibly something like this at some point after getting whatever event (keypress, menu click, etc) indicating that content should be pasted to your grid?

var clipData = Clipboard.GetData(DataFormats.Text).ToString();
if (string.IsNullOrEmpty(clipData))
    return;

var dataSource = new DataTable("data");
dataSource.Columns.Add("lines"); 

foreach(var line in clipData.Split('\n'))
    dataSource.Rows.Add(line);

datagrid.DataSource = dataSource;

This doesn't account for all the variety that can come in from the clipboard I imagine, but covers the basic concept of splitting text data from the clipboard by linebreak and creating multiple rows from that.

Upvotes: 2

Related Questions