问题
In my previous question, I posted a code that adds a table (not actually, just adds data to the richtxtbx from dgvw) in a rich text box ..The code is:
For i As Integer = 0 To dg2.Rows.Count - 2
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "-"
For j As Integer = 0 To dg2.Columns.Count - 1
Try
SendMail.bodytxt.Text = SendMail.bodytxt.Text + vbTab + dg2.Rows(i).Cells(j).Value.ToString() + vbTab
Catch ex As Exception
End Try
SendMail.bodytxt.Text = SendMail.bodytxt.Text + vbLf
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "--------------------------------------------" + vbLf
SendMail.bodytxt.Text = SendMail.bodytxt.Text + vbLf
Next
Next
The results look like this :
--------------------------------------------
88795446
--------------------------------------------
Mr.
--------------------------------------------
Raiyan
--------------------------------------------
rashid
--------------------------------------------
Male
--------------------------------------------
I hope you see what the problem is.. Each cell's value is being added in a new line.. How do I generate a view like this:
HEADER TEXT HEADER TEXT HEADER TEXT
------------------------------------------------
Mr. ABC 0123456789
------------------------------------------------
Mr. DEF 987654321
------------------------------------------------
Mr. GHI 898989898
I mean all cell values of a row would be in one line... The next line would be filled with dashes..... Then the next will contain the cell values of the next rows....
I know my code is correct but maybe I am having some "ARRANGING THE CODE" issue... Or is this happening because of my rich textbox size? The reason why I'm thinking so is because when I add data from a dgvw containing 2/3 columns, it works as expected.. So how to fix it or achieve the look I am looking for?
回答1:
maybe this would help
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "------------------------------------------------------------------------------------------" + vbLf
For i As Integer = 0 To dg2.Rows.Count - 1
For j As Integer = 0 To dg2.Columns.Count - 1
Try
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "• " + dg2.Rows(i).Cells(j).Value.ToString() + " •"
Catch ex As Exception
End Try
Next
SendMail.bodytxt.Text = SendMail.bodytxt.Text + vbLf
SendMail.bodytxt.Text = SendMail.bodytxt.Text + "------------------------------------------------------------------------------------------" + vbLf
Next
This might even give you a better look.You must add as many "-"(dashes)as you need.Let me know if it worked
回答2:
As you mentioned that when you add data to the rich text box from a datagridview that has a few columns..and then you get the result as desired..Then why not setting the WordWarp property of the Rich-text-box property to true and then add the data. Then i guess a row's cells' value wouldn't go to the next line and maybe you would be able to generate the look you want.
来源:https://stackoverflow.com/questions/47366431/table-in-richtextbox