4

Is it possible to in anyway display content in a windows form application based on how many rows there are in a database. I have no idea if this is possible in a simple way, but the concept is attached below which show the content section, the left side is an image and label2 should be the name for the person and label 3 is the content that the person writes.

Now the question is if there are more than one post, how can I make the "rows" show for all the content from a database. If I would have used ASP.NET it would probably be easier because the format would be:

<ul>
  <li><img src="#"/> <h1>Name</h1> <p>Content<p> </li>
<ul>

enter image description here

DLeh
  • 23,806
  • 16
  • 84
  • 128
btmach
  • 375
  • 1
  • 7
  • 16

2 Answers2

4

You should prepare a user control with your "post" look And next use FlowLayoutPanel as container and add to his Children your "Control" created for each post from db.

Edit: If you set FlowLayoutPanel.FlowDirection to TopDown you will get what you are looking for.

roxik0
  • 97
  • 6
2

What you're looking for is a ListView control. You will create a loop and add items for each row returned from the database. Check out Using a ListView with images for an example.

Edit: using a DataGridView will allow you to have multilines in each cell. Here's a quick example, I don't have a small image handy but this will set your text properly:

DataGridView dgv = new DataGridView();
dgv.Columns.Add("Header","Header");
dgv.Columns.Add("Details", "Details");
dgv.Dock = DockStyle.Fill;
dgv.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgv.Rows.Add(new string[] { "Drew", "Some details" + Environment.NewLine + "use two lines" });
dgv.Rows.Add(new string[] { "Bill", "More details" });
Controls.Add(dgv);
Community
  • 1
  • 1
DrewJordan
  • 5,266
  • 1
  • 25
  • 39
  • It's worth noting that this would really require a third-party version of `ListView` with multi-line support to not look like a total bodge. – helrich Jan 22 '15 at 13:58
  • yes, you're right, I overlooked that. a DataGridView will allow multi-lines in a single column, here's a post that asks the same question: http://stackoverflow.com/questions/22103749/multiline-text-in-list-view. Although, roxik0's solution is probably more flexible if the format is going to vary at all. – DrewJordan Jan 22 '15 at 14:10
  • I don't want to use the "table"-layout, I would somehow want to customize it to suite the layout and be similar to the html that I wrote. As example think about the twitter desktop application which show multiple posts with photos, name, time, content and line-seperator for each post? – btmach Jan 22 '15 at 14:27
  • you can customize the DataGridView to look like this too, for example, doing something like this: `dgv.CellBorderStyle = DataGridViewCellBorderStyle.None; dgv.RowHeadersVisible = false; dgv.ColumnHeadersVisible = false; foreach (DataGridViewRow row in dgv.Rows) { row.DefaultCellStyle.BackColor = Color.DarkGray; }` makes it look less like a grid... you might also look into using WPF with XAML which I'm not familiar with, but seems to work more like HTML. – DrewJordan Jan 22 '15 at 14:39
  • or, go with roxik0's solution and create your own user control. If you're looking for a fairly custom implementation, which it sounds like you may be – DrewJordan Jan 22 '15 at 14:50
  • Indeed, this is the kind of thing WPF/XAML lives for. If you are in a business environment where you don't have a choice, do the best you can, otherwise learn WPF. – Spiked3 Jan 22 '15 at 15:04