Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > C# Create a string out of a single column
 

C# Create a string out of a single column

i want to paste all the strings from a datatable collumn into one single string which is not in the datatable
i want the strings to be pasted in the single string in the right order
that't from top row to the row on the bottom so the string in the top row will be at the beginning of the single string en the row on the bottom at the end
my question is: what is the best way to do this?
i don't know if it matters but i'm using Visual C# Express 2008

Jelte12345  Thursday, October 01, 2009 8:01 PM

Hi

Simply loop in the rows of datatable and concat values of desired column and assign it to your variable.

Instead of concatation with + use StringBuilder. 

Here is a sample method.

        public string ColumnToString(DataTable dt, string columnName)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append(dr[columnName].ToString());
            }
            return sb.ToString();
        }
  • Marked As Answer byJelte12345 Friday, October 02, 2009 9:50 AM
  •  
Tamer Oz  Friday, October 02, 2009 3:13 AM

Hi

Simply loop in the rows of datatable and concat values of desired column and assign it to your variable.

Instead of concatation with + use StringBuilder. 

Here is a sample method.

        public string ColumnToString(DataTable dt, string columnName)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append(dr[columnName].ToString());
            }
            return sb.ToString();
        }
  • Marked As Answer byJelte12345 Friday, October 02, 2009 9:50 AM
  •  
Tamer Oz  Friday, October 02, 2009 3:13 AM

You can use google to search for other answers

Custom Search

More Threads

• Prevent insert in a datagrid
• DataGridViewCheckBoxColumn with Text Information
• Using Datatable with datagrid
• inserting cell values in DataGridView manually one at a time
• DataGridView update to database
• Cannot Save or Delete related tables
• Sort and filter with datagridview
• DataGrid Question - Using a column in a selected row - Newbie
• Current Control doesn't refresh values due to PropertyChanged event
• Binding source doesn't update dataset underlying