2010-12-08 5 views
0
Hi All, 

    I am currently binding my data in a datagrid like this 

     public DataTable GetAllPrimaryKeyTables(string localServer, string userName, string password, string selectedDatabase) 
      { 

       // Create the datatable 
       DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames"); 

       SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder(); 
       objConnectionString.DataSource = localServer; ; 
       objConnectionString.UserID = userName; 
       objConnectionString.Password = password; 
       objConnectionString.InitialCatalog = selectedDatabase; 

       // Query to select primary key tables. 
       string selectPrimaryKeyTables = @"SELECT 
                 TABLE_NAME 
                 AS 
                 TABLES 
                FROM 
                 INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
                WHERE 
                 CONSTRAINT_TYPE = 'PRIMARY KEY' 
                AND 
                 TABLE_NAME <> 'dtProperties' 
               ORDER BY 
                 TABLE_NAME"; 

       // put your SqlConnection and SqlCommand into using blocks! 
       using(SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString)) 
       using(SqlCommand sCommand = new SqlCommand(selectPrimaryKeyTables, sConnection)) 
       { 
        try 
        { 
         // Create the dataadapter object 
         SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeyTables, sConnection); 

         // Fill the datatable - no need to open the connection, the SqlDataAdapter will do that all by itself 
         // (and also close it again after it is done) 
         sDataAdapter.Fill(dtListOfPrimaryKeyTables); 
        } 
        catch(Exception ex) 
        { 
         //All the exceptions are handled and written in the EventLog. 
         EventLog log = new EventLog("Application"); 
         log.Source = "MFDBAnalyser"; 
         log.WriteEntry(ex.Message); 
        } 
       } 

       // return the data table to the caller 
       return dtListOfPrimaryKeyTables; 



      } 

And then giving the datasource as this 

    protected void GetPrimaryKeyTables() 
     { 
      DataTable dtPrimaryKeys = new DataAccessMaster().GetAllPrimaryKeyTables(txtHost.Text, txtUsername.Text, txtPassword.Text, Convert.ToString(cmbDatabases.SelectedValue)); 
      dgResultView.DataSource = dtPrimaryKeys; 
     } 

But now I need to bind the datatable to a richtextbox control available in the toolbox. 

Waiting for reply!!! 

どうすればいいですか?リッチテキストボックスコントロールへのデータのバインドi Windowsアプリケーション

+0

メッセージを正しくフォーマットすることを検討してください。非コードをコードブロックの外に置きます。また、あなたのコードをそのまま "そのまま"放置しないでください。具体的にし、質問の可読性を向上させるために関連する部分のみを投稿してください。 –

答えて

0

2つの要素の構造が本質的に異なるため、DataTableをRichTextBoxに「そのまま」バインドすることはできません。マルチエレメント表を単一のエレメント文字列を含むエレメントに「魔法のように」バインドすることはできません。あなたはDBから文字列を抽出し、RichTextBoxコントロールに挿入するか、文字列としてバインドする必要があります。

関連する問題