我有一个从CSV文件填充的DataTable,然后使用DataGridView在内存中编辑数据。据我所知,数据的编程编辑应该在用户编辑通过的DataTable上完成。 DataGridView。将列添加到绑定到DataGridView的DataTable不会更新视图
但是,当我以编程方式向DataTable添加列时,它不会自动反映在DataGridView中,我怀疑反过来也是如此。
你如何保持两个并发?我认为数据绑定是,这是自动的想法...
下面是相关设置代码 - WorksheetGridView
子DataGridView
// Can access data directly
public DataTable data = new DataTable();
public WorksheetGridView()
{
InitializeComponent();
// Allow copying from table to clipboard
this.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
// TODO: how to allow both row and column selects?
//this.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
// Load up a blank DataTable to hold user inputted data
int i;
const int numBlankRows = Config.Application.DefaultNumRows;
const int numBlankCols = Config.Application.DefaultNumCols;
// TODO: Figure out how to include this as a config variable
DBNull dfltCellContent = DBNull.Value;
DataRow tmpRow;
// Add columns - i used for naming
for (i = 0; i < numBlankCols; i++)
{
this.AddColumn(i);
}
// Add rows
for (i = 0; i < numBlankRows; i++)
{
tmpRow = this.data.NewRow();
// Fill cells with something (i.e. blank cells)
foreach (DataColumn col in this.data.Columns)
{
tmpRow[col.ColumnName] = DBNull.Value;
}
this.data.Rows.Add(tmpRow);
}
// Link data to the view
this.DataSource = this.data;
}
private void AddColumn(int colIndex)
{
// Adds a column to the data array
DataColumn tmpCol;
tmpCol = new DataColumn();
tmpCol.DataType = Type.GetType(Config.Application.DataType);
tmpCol.ColumnName = "C" + colIndex.ToString();
tmpCol.ReadOnly = false;
tmpCol.Unique = false;
tmpCol.AllowDBNull = true;
this.data.Columns.Add(tmpCol);
}
不起作用该位后,当我打电话AddColumn
为在这个代码中的实例来处理粘贴制表符分隔的数据,
public void PasteToCells(string mode)
{
// TODO: Write paste code
int i;
if (Clipboard.ContainsText())
{
string clipBoardContent = Clipboard.GetText();
using (CsvReader pastedCsvReader = new CsvReader(
new StringReader(clipBoardContent), false, '\t'))
{
// TODO: If more rows/cols than in data table then expand accordingly
int numPastedCols = pastedCsvReader.FieldCount;
int currRowIndex, currColumnIndex;
GetSelectedInsertPoint(out currRowIndex, out currColumnIndex);
// Make space for columns if needed
if (mode == "insertcols")
{
int baseIndex = this.data.Columns.Count;
for (i = 0; i < numPastedCols; i++)
{
//this.Columns.Add
// TODO: Doesn't work yet - do you edit the DataGridView and reflect to DataTable or vise-versa?
this.AddColumn(baseIndex + i);
}
}
while (pastedCsvReader.ReadNextRecord())
{
// Make space for rows (row by row) if needed
if (mode == "insertrows")
{
this.data.NewRow();
// TODO: Add a row
}
// Populate the cells with valid pasted text
for (i = 0; i < numPastedCols; i++)
{
this.data.Rows[currRowIndex][currColumnIndex + i] = ConvertCellContent(pastedCsvReader[i]);
}
currRowIndex = currRowIndex + 1;
}
}
}
else
{
// TODO: Do nothing?
Console.WriteLine("No text on clipboard");
}
}
我试过下面给出的例子,它的工作。但是,当我尝试执行此操作时,水平滚动条会短暂扩展并收缩,但子类型DataGridView
中的表未更新。不过,该列在DataTable
之间 - 例如,我无法添加同名的列,列数也反映了额外的列。有没有设计者可能会限制更新DataGridView
?
另外添加行工作正常。
解决:
的AutoGeneratedColumns
被设置为在设计代码false
尽管是在属性对话框true
(和在代码中明确设置)。最初的列是以编程方式生成的,所以不应该出现,但是由于设计器代码还继续生成最初用于调试的“设计为”列,因此未能提供。
道德:检查自动生成的代码!
特别是检查设计器代码中的AutoGenerateColumns值。由于该示例在调试中帮助了很多人,因此选择了答案! – Brendan 2009-01-31 16:29:29