簡體   English   中英

DataGridView不顯示DataTable

[英]DataGridView does not display DataTable

我有以下代碼,我認為應該將DataTable綁定到DataGridView,但是DataGridView顯示為空。 DataTable肯定有行,因此我假設我以某種方式錯誤地綁定了DataSource。 有誰看到這是怎么回事:

DataBase db = new DataBase(re.OutputDir+"\\Matches.db");
MatchDBReader reader = new MatchDBReader(db.NewConnection());

BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = reader.GetDataTable();

this.dataGridView1.DataSource = bindingSource.DataSource;
  • 第一行只是獲取我要從中提取數據的數據庫的句柄。
  • 下一行是提供一個用於從同一數據庫讀取的類-特別是它公開了GetDataTable方法,並返回了我打算放入DataGridView的數據表。
  • 下一行沒意思...
  • 第四行嘗試獲取DataTable-QuickWatch表示此方法正在運行...
  • 最后一行是我認為自己搞砸了的地方……我的理解是,這將DataTable綁定到了DataGridView GUI,但沒有任何顯示。

有什么想法嗎?

嘗試將DataGridView直接綁定到BindingSource,而不是BindingSource的DataSource:

this.dataGridView1.DataSource = bindingSource;

在獲取數據表之前,需要將BindingSource附加到網格。

嘗試切換最后兩行代碼:

DataBase db = new DataBase(re.OutputDir+"\\Matches.db");
MatchDBReader reader = new MatchDBReader(db.NewConnection());
BindingSource bindingSource = new BindingSource();
this.dataGridView1.DataSource = bindingSource.DataSource;
bindingSource.DataSource = reader.GetDataTable();

除上述解決方案外,還修復了“ bindingSource”數據成員屬性。 喜歡:

bindingSource.DataMember = yourDataSet.DataTable;

我在sql數據庫和datagrid視圖中遇到了相同的問題。 經過很多麻煩之后,我發現我忘記設置綁定源的dataMember屬性。

祝你好運。

盡管這一切似乎都是很好的建議,但這些對我都不起作用。 我最終要做的是地球上最大,最糟糕的黑客事件。 我希望完成的只是從SQLite數據庫加載數據庫表,並在DataGridView中顯示它(只讀,帶有可排序的列)。 實際的數據庫將在運行時以編程方式指定。 我通過將DataGridView添加到表單中來定義DataSet,並使用向導靜態定義了數據庫連接字符串。 然后,我進入Settings.Designer.cs文件,並將一個set訪問器添加到DB Connection字符串屬性:

namespace FormatDetector.Properties {


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        [global::System.Configuration.DefaultSettingValueAttribute("data source=E:\\workspace\\Test\\Matches.db;useutf16encoding=True")]
        public string MatchesConnectionString {
            get {
                return ((string)(this["MatchesConnectionString"]));
            }
            set
            {
                (this["MatchesConnectionString"]) = value;
            }
        }
    }
}

這是一個笨拙的技巧,但確實有效。 關於如何清理這些混亂的建議非常受歡迎。

布賴恩

DataGridViewDataTable為基礎。 DataTable列將其類型另存為屬性。

如果此類型是接口,您將看到的都是空單元格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM