我正在尝试将任何CSV / Excel文件中的数据填充到gridview中。当所有列都可见时,它会上传很好。但是,我根据用户的选择显示/隐藏网格中的列。所以我希望我粘贴的值只填充在网格的可见列中。我在winforms工作。我已将我用于将数据粘贴到网格下面的代码
//Code for pasting clipboard data into gridview
private void PopulateImportGrid()
{
char[] rowSplitter = {'\r', '\n'};
char[] columnSplitter = {'\t'};
//get the text from clipboard
IDataObject dataInClipboard = Clipboard.GetDataObject();
if (dataInClipboard != null)
{
var stringInClipboard = (string) dataInClipboard.GetData(DataFormats.Text);
//split it into lines
string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
// grdSiteDetails.DeferRefresh();
// loop through the lines, split them into cells and place the values in the corresponding cell.
using (grdSiteDetails.DeferRefresh())
{
for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
{
grdSiteDetails.Rows.Add(iRow);
//split row into cell values
string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);
//cycle through cell values
for (int iCol = 0; iCol < valuesInRow.Length; iCol++)
{
//assign cell value, only if it within columns of the grid
if (grdSiteDetails.ColumnCount - 1 >= iCol)
{
grdSiteDetails.Rows[iRow].Cells[iCol].Value = valuesInRow[iCol];
}
}
}
}
}
grdSiteDetails.ColumnChooserSortOrder = RadSortOrder.None;
}`
答案 0 :(得分:0)
如何隐藏列?如果您使用以下物业。粘贴时检查相同的属性。
this.dataGridView1.Columns [&#34; COLUMNNAME&#34]。可见