Привязка данных к элементу управления rich textbox в приложении Windows - PullRequest
0 голосов
/ 08 декабря 2010
    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 голосов
/ 08 декабря 2010

Я не думаю, что вы можете связать dataTable с RichTextBox "как есть", потому что структура этих двух элементов по сути различна. Вы не можете «волшебным образом» связать многоэлементную таблицу с одним элементом, содержащим строку элемента. ИМХО, вы должны извлечь строку из БД и поместить ее (или связать как строку) в свой элемент управления RichTextBox.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...