[英]Data from another class cannot be put to a datagridview
美好的一天。 我已經將數據變量從一個類傳遞到另一個類,以主要形式放入datagridview中。 我在每種情況下都放了一些消息框,以了解它訪問了所述功能,並且清楚地傳遞了數據。 但是當我運行程序時。 該表未將數據放入其中。
這是我傳遞數據時的代碼
if (txtCode1.ElementAt(intCtr + 1).Equals(val4)) {
MessageBox.Show("Lol");
Compilourdes_GUI cmp = new Compilourdes_GUI();
cmp.AddtotblLexeme(val2, val2);
break;
}
這是AddtotblLexeme的代碼
public void AddtotblLexeme(string lexeme, string token) {
MessageBox.Show(lexeme+" "+token);
tblLexeme.Rows.Add(lexeme , token); //adding tokens and lexeme to the table
}
我制作數據表的代碼
private void Start()
{
tbl1.AutoGenerateColumns = true;
tbl1.DataSource = null;
tbl1.Rows.Clear();
InitTable();
string txtCode1 = txtCode.Text;
LexicalAnalyzer lex = new LexicalAnalyzer(txtCode1);
lex.StartLex();
tbl1.DataSource = tblLexeme;
}
public void InitTable()
{
tblLexeme = new DataTable();
tblLexeme.Columns.Add("Lexeme", typeof(string));
tblLexeme.Columns.Add("Token", typeof(string));
}
DataTable tblLexeme = new DataTable();
這是輸出的圖像 。 “ TEST”一詞應該在表格中,但是如您所見,它沒有被輸入。
好的,我想我理解您的問題。 如果您直接在設計器中添加了列,我的猜測是您添加了未綁定的列。 如果是這樣,則DataGridView無法將您要添加的行與表中的行匹配。 要解決此問題,請從DatagridView中刪除列。 然后,在設置DataSource = tblLexeme之前,請確保您的DataGridView具有屬性AutoGenerateColumns = true。 現在自動發生兩件事:首先,DataGridView從您的DataTable中獲取列; 其次,在向DataTable添加新行時,該行應自動顯示在DataGridView中。
在AddtotblLexeme中,出於測試目的,您能否代替Rows.Add()添加:
DataRow nR = tblLexeme.NewRow();
nR[0] = lexeme;
nR[1] = token;
tblLexeme.Rows.Add(nR);
然后在調試器中檢查nR的確有2列的ItemArray。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.