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

어떻게해야합니까 ??데이터를 서식있는 텍스트 상자 컨트롤에 바인딩 Windows 응용 프로그램

+0

메시지를 올바르게 포맷하는 것을 고려하십시오. 코드 블록 외부에 코드가 아닌 것을 넣으십시오. 게다가, 모든 코드를 "있는 그대로"드롭하지 마십시오. 질문의 가독성을 높이기 위해 구체적으로 관련 부분 만 게시하십시오. –

답변

0

두 요소의 구조가 본질적으로 다르기 때문에 "있는 그대로"RichTextBox에 dataTable을 바인딩 할 수 있다고 생각하지 않습니다. 다중 요소 테이블을 단일 요소 문자열 포함 요소에 "마술처럼"바인딩 할 수 없습니다. IMHO 당신은 DB에서 문자열을 추출하여 RichTextBox 컨트롤에 넣거나 문자열로 바인딩해야합니다.

관련 문제