将数据绑定到富文本框控件i windows应用程序

时间:2010-12-08 07:19:47

标签: c# visual-studio winforms controls

    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!!!

怎么做?

1 个答案:

答案 0 :(得分:0)

我认为你不能将dataTable“按原样”绑定到RichTextBox,因为这两个元素的结构本质上是不同的。你不能“神奇地”将多元素表绑定到包含单个元素字符串的元素。恕我直言,您应该从数据库中提取字符串并将其放入(或将其作为字符串绑定)您的RichTextBox控件。