2010-12-08 7 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!!! 

Comment faire cela?Liaison de données à un contrôle de zone de texte enrichi i application Windows

+0

Veuillez envisager de formater votre message correctement.Mettez le non-code en dehors des blocs de code. En outre, ne laissez pas tomber tout votre code "tel quel". Soyez précis et affichez uniquement les parties pertinentes pour améliorer la lisibilité des questions. –

Répondre

0

Je ne pense pas que vous pouvez lier un dataTable à un RichTextBox "tel quel", car la structure des deux éléments est intrinsèquement différente. vous ne pouvez pas "magiquement" lier une table multi-éléments à un seul élément contenant une chaîne d'éléments. À mon humble avis, vous devez extraire la chaîne de DB et le mettre en (ou le lier en tant que chaîne) votre contrôle RichTextBox.

Questions connexes