我想使用C#在WPF中逐行读取dataGrid

时间:2018-07-19 04:56:59

标签: c# xml

Workbook workbook = new Workbook();
workbook.LoadFromFile(FileNameDb);
Worksheet sheet = workbook.Worksheets[0];
DataTable dataTable = sheet.ExportDataTable();
DataView view = new DataView(dataTable);
dataGridView1.ItemsSource = view;

string str;
for (int i = 0; i < dataGridView1.Items.Count - 1; i ++)
{
str = "Insert Into Sample(MCC, MNC, LAC, CellId, CellIdAddress, LAT, LONG, Network) 
                   Values(" + dataGridView1.Rows[i].Cells[0].Value.ToString() + ", '" + 
                              dataGridView1.Rows[i].Cells[1].Value.ToString() + "'," + 
                              dataGridView1.Rows[i].Cells[2].Value.ToString() + "," + 
                              dataGridView1.Rows[i].Cells[3].Value.ToString() + "," + 
                              dataGridView1.Rows[i].Cells[4].Value.ToString() + "," + 
                              dataGridView1.Rows[i].Cells[5].Value.ToString() + "," + 
                              dataGridView1.Rows[i].Cells[6].Value.ToString() + "," + 
                              dataGridView1.Rows[i].Cells[7].Value.ToString() + ")";
    command = new SQLiteCommand(str, m_dbConnection);
    command.ExecuteNonQuery();
}

但是这里的行给我错误数据网格不包含行的定义 在读取了每一行之后,将该行添加到sqlite数据库中

2 个答案:

答案 0 :(得分:0)

以下代码有望正常工作

foreach (DataGridCellInfo di in dgDataGrid.SelectedCells) 
{
    DataRowView dvr = (DataRowView)di.Item;

    MessageBox.Show(dvr[1].ToString()); 
}

答案 1 :(得分:0)

在datagrid之类的循环上使用

for(int i = 0; i <= datagrid.Items.length; i ++)    {      字符串名称=      ((您的对象)datagrid.Items [i])。name;    }