Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView Layout Problem

Status
Not open for further replies.

GavW

Programmer
Jan 13, 2006
58
GB
Hey all!

I would like to display multiple records on a single GridView row. The standard layout looks like the following:

Record1
Record2
Record3
Record4

However I would like my GridView to display like:

Record1 Record2
Record3 Record4

What do I need to change in order to acheive this result?

Thanks
 
One approach would be to create a DataTable with two coulmns and populate the table from the DataSet/Reader.
 
hmmm not familiar with that approach. Anything that can help me implement this method?
 
Here is a sample datagrid with two columns.

Code:
            <asp:datagrid id="dgPhotos" runat="server" GridLines="None" AutoGenerateColumns="False" CellPadding="3" CellSpacing="3" Width="454">
				<ItemStyle BackColor="#f2f2f2" HorizontalAlign="center" VerticalAlign="Middle" Height="180px" CssClass="body2"/>
				<Columns>
					<asp:TemplateColumn ItemStyle-Width="50%">
						<ItemTemplate>
							<%# DataBinder.Eval(Container.DataItem, "1")%>
						</ItemTemplate>
					</asp:TemplateColumn>
					<asp:TemplateColumn ItemStyle-Width="50%">
						<ItemTemplate>
							<%# DataBinder.Eval(Container.DataItem, "2")%>
						</ItemTemplate>
					</asp:TemplateColumn>					
				</Columns>
			</asp:datagrid>

This is the code that takes my returned list of photos and places them into a DataTable with 2 columns, then binds the Datatable to the DataGrid

Code:
        DataSet ds;
        //Fill Dataset
        ds = DatabaseFunctions.GetUserPhotos(iGID);


        string[] pictureUrl = new string[20];
        int counter = 0;

        //Create an image path
        foreach (DataRow dr in ds.Tables[0].Rows)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("<img src='images/uploaded/");
            sb.Append(dr["path"].ToString());
            sb.Append("' border='0'>");
            sb.Append("<br />");

            if (Convert.ToInt32(dr["defaultphoto"]) == 1)
            {
                sb.Append("Default Profile Image | ");

            }
            else
            {
                sb.Append("<a href='myProfilePhotos.aspx?SetDefault=");
                sb.Append(dr["id"].ToString());
                sb.Append("'>Make Default</a> | ");
            }

            sb.Append("<a href='myProfilePhotos.aspx?Delete=");
            sb.Append(dr["id"].ToString());
            sb.Append("'>Delete</a>");
            pictureUrl[counter] = sb.ToString();

            counter += 1;

        }

        int N = ds.Tables[0].Rows.Count;
        int numRows;
        int numCols = 2;
        int numLastLine = N % numCols;

        if (numLastLine == 0)
        {
            numRows = N / 2;
        }
        else
        {
            numRows = (int)N / 2 + 1;

        }
        if (N > 0)
        {
            DataRow row;
            DataTable imageTable = new DataTable();

            imageTable.Columns.Add("1", Type.GetType("System.String"));
            imageTable.Columns.Add("2", Type.GetType("System.String"));


            for (int I = 0; I < numRows; I++)
            {
                row = imageTable.NewRow();

                if (I == numRows)
                {
                    numCols = numLastLine;
                }
                else
                {
                    numCols = 2;
                }
                for (int J = 1; J < numCols + 1; J++)
                {
                    row[J.ToString()] = pictureUrl[2 * I + (J - 1)];
                }
                imageTable.Rows.Add(row);
            }

            dgPhotos.DataSource = imageTable;
            dgPhotos.DataBind();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top