in my access database the column "foods" has multiline text..but when i view it in datagridview in my c# form it only has single line text,,, the width and height in datagridview is not the same as in my access database...

my access database looks like this

|primary key||foods||servings|
00000001 | chicken 11/2
fish 1
egg 1

my output in my c# forms looks like this

primarykey|food |servings|
00000001 chickenIIfish 11/211


how can i view my database in datagridview with the same witdth and height as my database is?? thanks..


heres my code in filling datagrid view

OleDbConnection conn1 = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\Anakz\My Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\PDG.mdb");
                    string query = "SELECT * FROM children";
                    //create an OleDbDataAdapter to execute the query
                    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, conn1);

                    //create a command builder
                    OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

                    //create a DataTable to hold the query results
                    DataTable dTable = new DataTable();

                    //fill the DataTable
                    dAdapter.Fill(dTable);
                    //the DataGridView
                   

                    //BindingSource to sync DataTable and DataGridView
                    BindingSource bSource = new BindingSource();

                    //set the BindingSource DataSource
                    bSource.DataSource = dTable;

                    //set the DataGridView DataSource
                    pf.dataGridView1.DataSource = bSource;

Recommended Answers

All 5 Replies

So:
chicken 11/2
fish 1
egg 1

are all in its lines in database?

Is your dataBase field type of varchar?
But it doesnt matter. You cannot declare any height and width of a cell.
YOu can only split text using some delimiters, which will later help you to split the text into multilines, or create a column width so only one word will go into one line, and then use property AutoResizeRow, and use AutoSizeRowsMode.

how can u do those delimiters? can u show me some code for reference??
where do i declare autosizerow and autosizerowsmode??? thanks..

Delimiter is some character which you pick it up by your self. Something that you know you have to use split string (if the string containg delimiters).
By default, you can write "\r\n" (no quotes), which is a literal to new line.
You can use some of your own, like comma, or semicolon.

So you do:

dataGridView1.Columns.Add("col1", "column 1");

            //INSERT:
            string a = "line 1";
            string b = "line 2";
            string c = "line 3";
            
            string delimiter = "\r\n";
            string textTogether = String.Join(delimiter, new string[] { a, b, c });
            //now do the insertion into dataBase (into one cell(column)).

            //then when you want to get it back and pass data into one column (into seperate lines):
            //get data back out of dataBase and from dataTable.
            string line = textTogether;
            //now:
            //string[] array = line.Split(',');
            dataGridView1.Rows.Add();
            dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            dataGridView1.Rows.Add(line);

this is what i've been looking for.. this thing works for me..

With prf.DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True

thanks a lot bonca...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.