It's possible to bind object to DataGridViewRow of DataGridView via Tag property:
var row = (DataGridViewRow)dataGridView1.RowTemplate.Clone();
row.Tag= new SomeClass();
But how to make some binding not with (DataGridViewRow)dataGridView1.RowTemplate.Clone()
, but by using DataTable
?
I've made custom MyDataRow
with public object Tag { get; set; }
public object Tag { get; set; }
public object Tag { get; set; }
:
[Serializable]
public class MyDataRow : DataRow
{
public object Tag { get; set; }
public MyDataRow()
: base(null)
{
}
public MyDataRow(DataRowBuilder builder)
: base(builder)
{
}
}
And MyDataTable
for it:
[Serializable]
public class MyDataTable : DataTable
{
public MyDataTable()
: base()
{
}
public MyDataTable(string tableName)
: base(tableName)
{
}
public MyDataTable(string tableName, string tableNamespace)
: base(tableName, tableNamespace)
{
}
/// <summary>
/// Needs using System.Runtime.Serialization;
/// </summary>
public MyDataTable(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
protected override Type GetRowType()
{
return typeof(MyDataRow);
}
protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
{
return new MyDataRow(builder);
}
}
After that I am trying to fill data:
MyDataTable dt = new MyDataTable();
MyDataRow dr = dt.NewRow() as MyDataRow;
dr.Tag = new SomeObject();
var arr = columns2.ToArray();
for (int x = 0; x < arr.Length; x++)
{
dr[x] = arr[x];
}
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
As result, there are no Tag values in DataGridView1 row
private void DataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
var row = dataGridView1.Rows[e.RowIndex];
var l = (List<string>)row.Tag; // <-- Here is Exception occures, because row.Tag comes Null
How can I pass Tag values of rows for DataGridView with DataSources?
The DataGridViewRow
's .Tag
is null
because you didn't set it to anything . The fact that your MyDataRow
has a .Tag
itself is irrelevant and unrelated - completely different .Tag
. One option would be to set the .Tag
of the actual DataGridViewRow
.
What you might want to do is look at the .DataBoundItem
of the row; this should be your MyDataRow
instance, but: it is possible that this will actually be the DataRowView
, in which case you'll want to look at the .Row
of that instead.
You probably might want to use separate column for "tag" purposes:
class EventsDataTable : DataTable
{
public DataColumn ColumnVariables { get; set; }
public DataColumn ColumnSource { get; set; }
public EventsDataTable()
{
ColumnSource = new DataColumn("Source", typeof(string),
null, MappingType.Element);
base.Columns.Add(ColumnSource);
ColumnVariables = new DataColumn("Variables", typeof(EventData),
null, MappingType.Hidden);
base.Columns.Add(ColumnVariables);
}
}
Then you can add rows and set a "raw" pointer to your fake column:
public void AddNewEvent(EventData eventData)
{
DataRow dataRow = _eventsTable.NewRow();
dataRow[_eventsTable.ColumnSource.ColumnName] = eventData.Source;
dataRow[_eventsTable.ColumnVariables.ColumnName] = eventData;
_eventsTable.Rows.Add(dataRow);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.