[英]pull data from a Datagridview in vb
我有我的datagridview,我想將該信息傳遞到數據表或其他地方,我正在嘗試
Dim dt As DataTable
dt = CType(DataGridView1.DataSource, DataTable)
但是我只會得到空值,有什么想法嗎? 我不知道如何從datagridview獲取信息
你可以試試看 dt = TryCast(DataGridView1.DataSource, DataTable)
或
Dim dt As New DataTable
dt = (DirectCast(DataGridView1.DataSource, DataTable))
要么
使用以下函數從網格獲取數據表
public DataTable DataGridView2DataTable(DataGridView dgv, String tblName, int minRow=0){
DataTable dt = new DataTable(tblName);
// Header columns
foreach (DataGridViewColumn column in dgv.Columns)
{
DataColumn dc = new DataColumn(column.Name.ToString());
dt.Columns.Add(dc);
}
// Data cells
for (int i = 0; i < dgv.Rows.Count; i++)
{
DataGridViewRow row = dgv.Rows[i];
DataRow dr = dt.NewRow();
for (int j = 0; j < dgv.Columns.Count; j++)
{
dr[j] = (row.Cells[j].Value == null) ? "" : row.Cells[j].Value.ToString();
}
dt.Rows.Add(dr);
}
// Related to the bug arround min size when using ExcelLibrary for export
for (int i = dgv.Rows.Count; i < minRow; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
dr[j] = " ";
}
dt.Rows.Add(dr);
}
return dt;
}
我有一個解決方案,有點臟,但是...對我有用
Dim valor As Integer
valor = DataGridView1.RowCount
valor = valor - 2
For indice As Integer = 0 To valor
' DataGridView1.Item(0, indice).Value.ToString()
' DataGridView1.Item(1, indice).Value.ToString()
' in my case i only have two colums but if tyou have more you can use double for
Next
希望這對某人有幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.