Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a DataGridView to display the contents of a DataTable
    primarykey
    data
    text
    <p>I have a three-column DataTable that contains data that I need to use in various ways. The data in this DataTable comes from an Exchange Web Services call. The first column contains a row counter, the second contains the calendar name, and the third contains the calendar ID (which is a gibberish-looking string).</p> <p>Displaying the raw data in a DataGridView is straightforward. But I want the DataGridView's second column to be different: instead of showing the calendar ID, I want to show some text ("Copy"), which, when clicked, will copy the calendar ID to the user's clipboard. Ideally it'd be styled like a hyperlink so the user knows it's not just some text in a box. This can conceivably be done with a DataGridViewLinkColumn type with an event handler.</p> <p>The piece I'm having trouble with is populating the rows.</p> <p>The following code sets up the columns just fine. But it doesn't populate the rows.</p> <pre class="lang-cs prettyprint-override"><code>private static DataGridView CreateCalendarGridView(DataTable calendarTable) { //Bind to the data source var calendarGridView = new DataGridView(); //calendarGridView.AutoGenerateColumns = false; calendarGridView.AutoSize = false; calendarGridView.DataSource = calendarTable; calendarGridView.ColumnHeadersVisible = true; //Create counter column var counterColumn = new DataGridViewTextBoxColumn(); counterColumn.DataPropertyName = COUNTER_COLUMN_NAME; counterColumn.Name = COUNTER_COLUMN_NAME; counterColumn.HeaderText = COUNTER_COLUMN_LABEL; //Create the Calendar name column var calendarNameColumn = new DataGridViewTextBoxColumn(); calendarNameColumn.DataPropertyName = CALENDAR_COLUMN_NAME; calendarNameColumn.Name = CALENDAR_COLUMN_NAME; calendarNameColumn.HeaderText = CALENDAR_COLUMN_LABEL; //Create the Copy ID hyperlink column var copyIdLinkColumn = new DataGridViewLinkColumn(); copyIdLinkColumn.DataPropertyName = ID_COLUMN_NAME; copyIdLinkColumn.Name = ID_COLUMN_NAME; copyIdLinkColumn.HeaderText = ID_COLUMN_NAME; copyIdLinkColumn.UseColumnTextForLinkValue = false; copyIdLinkColumn.TrackVisitedState = true; copyIdLinkColumn.LinkColor = Color.Blue; copyIdLinkColumn.VisitedLinkColor = Color.Purple; copyIdLinkColumn.LinkBehavior = LinkBehavior.HoverUnderline; copyIdLinkColumn.Text = "Copy"; copyIdLinkColumn.UseColumnTextForLinkValue = true; calendarGridView.Columns.Add(counterColumn); calendarGridView.Columns.Add(calendarNameColumn); calendarGridView.Columns.Add(copyIdLinkColumn); //I feel like I'm missing a foreach row in the DataTable which would //create the row of cells, but I'm not sure, because isn't that the //point of the calendarGridView.DataSource assignment above? return calendarGridView; } </code></pre> <p>I feel like if I could get the rows to fill, I could figure out how to change the behavior of the clickable third column.</p> <p>What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload