Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / September 2005

Tip: Looking for answers? Try searching our database.

rotate a datagrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joey - 13 May 2004 15:11 GMT
hi everyone

is it possible to rotate a datagrid, so that the columns become rows and
the rows columns?

so the datagrid shows like

-----------------
| header | data |
-----------------
| header | data |
-----------------

or is there another way to make columns editable instead of rows ?

any help appreciated,

Joey
Alvin Bruney [MVP] - 13 May 2004 15:28 GMT
here is some code:

Sometimes it is necessary to flip a datagrid's rows with its columns. Here
is the code:

Let's assume the information is returned in a dataset called ds. Here is how
we fix that puppy.

DataSet dsTemp = new DataSet();

DataTable Tables = new DataTable();

dsTemp.Tables.Add(Tables);

dsTemp.Tables[0].Columns.Add( " ", System.Type.GetType( "System.String" ) );

dsTemp.Tables[0].Columns.Add( "% variance", System.Type.GetType(
"System.String" ) );

.

.

.

[snip]

                       try

                       {

                                   for(int col = 0; col <
ds.Tables[0].Columns.Count; col++)

                                   {

                                               DataRow myRow =
dsTemp.Tables[0].NewRow();

                                               myRow[1]=
ds2.Tables[0].Rows[0][col].ToString();

                                               myRow[0]=
ds.Tables[0].Columns[col].ColumnName;

.

.

.

[snip]

                                               dsTemp.Tables[0].Rows.Add(myRow);

                                   }

                       }

                       catch(Exception ex)

                       {

                                   [snip]

                       }

                       if(ds.Tables[0].Rows.Count > 0)

                       {

                                   // FlippedDataGrid. Is our datagrid

                                   FlippedDataGrid.DataSource      =
dsTemp;

                                   FlippedDataGrid..DataBind();

                       }

Signature

Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok

> hi everyone
>
[quoted text clipped - 14 lines]
>
> Joey
Steven Cheng[MSFT] - 14 May 2004 09:41 GMT
Hi Joey,

In addition to Saravana, Alvin's suggestion on do the rotate at the
datasource level( switch the datas in the DataSet), here are my suggestions;

As for your condition,you're not only want to display horizentally, but
also perform the horizental mode at edit mode, so I think the DataGrid web
server control is not very suitable. I'd recommend you try the DataList
control
The DataList Control has  a "RepeatDirection" property which is used to
speicify the direction of the repeated items in the DataList, we can assign
the "RepeatDirection.Horizontal" as its value so as to let the items
repeated horizentally. Also, we need to customize the ItemTemplate and
EditTemplate and manually add the CommandButtons in DataList. For detailed
reference , you can view the following link in MSDN:
#DataList.RepeatDirection Property
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuiwebcontro
lsdatalistclassrepeatdirectiontopic.asp?frame=true

To make it clearly, here is a simple demo page, you may have a look :

==================aspx page======================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>DataList</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <table width="100%" align="center">
                    <tr>
                        <td><asp:datalist id="dlMain" runat="server"
RepeatDirection="Horizontal">
                                <HeaderTemplate>
                                    <table align="center" width="100%">
                                        <tr>
                                </HeaderTemplate>
                                <FooterTemplate>
                    </tr>
            </table>
            </FooterTemplate>
            <ItemTemplate>
                <td>
                    <table>
                        <tr>
                            <td><%# DataBinder.Eval(Container.DataItem,"index") %></td>
                        </tr>
                        <tr>
                            <td><%# DataBinder.Eval(Container.DataItem,"name") %></td>
                        </tr>
                        <tr>
                            <td><%# DataBinder.Eval(Container.DataItem,"email") %></td>
                        </tr>
                        <tr>
                            <td><asp:Button ID="btnEdit" Runat="server" Text="Edit"
CommandName="Edit" CommandArgument='<%# Container.ItemIndex
%>'></asp:Button></td>
                        </tr>
                    </table>
                </td>
            </ItemTemplate>
            <EditItemTemplate>
                <td>
                    <table>
                        <tr>
                            <td><asp:TextBox ID="txtIndex" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"index") %>'></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td><asp:TextBox ID="txtName" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"name") %>'></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td><asp:TextBox ID="txtEmail" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"email") %>'></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Button ID="btnUpdate" Runat="server" Text="Update"
CommandName="Update" CommandArgument='<%# Container.ItemIndex %>'>
                                </asp:Button>
                                <asp:Button ID="btnCancel" Runat="server" Text="Cancel"
CommandName="Cancel" CommandArgument='<%# Container.ItemIndex %>'>
                                </asp:Button>
                            </td>
                        </tr>
                    </table>
                </td>
            </EditItemTemplate>
            </asp:DataList></TD></TR>
            <tr>
                <td></td>
            </tr>
            </TABLE></form>
    </body>
</HTML>

================code behind page classs============
public class DataList : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataList dlMain;
   
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!IsPostBack)
            {
                Bind_Data();
            }
        }

        private void Bind_Data()
        {
            DataTable tb = new DataTable("user");
            tb.Columns.Add("index",typeof(int));
            tb.Columns.Add("name",typeof(string));
            tb.Columns.Add("email",typeof(string));

            DataRow dr = null;

            for(int i=1;i<=6;i++)
            {
                dr = tb.NewRow();
                dr["index"] = i;
                dr["name"] = "Name" + i;
                dr["email"] = "User" + i + "@test.com";
                tb.Rows.Add(dr);
            }

           
            dlMain.DataSource = tb;
            dlMain.DataBind();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.dlMain.CancelCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.dlMain_CancelComm
and);
            this.dlMain.EditCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.dlMain_EditComman
d);
            this.dlMain.UpdateCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.dlMain_UpdateComm
and);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

       

       

        private void dlMain_EditCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
        {
            dlMain.EditItemIndex = e.Item.ItemIndex;
            Bind_Data();
        }

        private void dlMain_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
        {
            dlMain.EditItemIndex = -1;
            Bind_Data();
        }

        private void dlMain_CancelCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
        {
            dlMain.EditItemIndex = -1;
            Bind_Data();
        }
    }
====================================================

Hope these helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Signature

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Signature

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Steven Cheng[MSFT] - 19 May 2004 01:18 GMT
Hi Joey,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Signature

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Signature

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Saravana [MVP] - 14 May 2004 05:15 GMT
Hi,

I thing its better to get the data in whatever format you need it from
datasource(for example sql server) instead of doing that with dataset.

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

> hi everyone
>
[quoted text clipped - 14 lines]
>
> Joey
thomas a - 26 Sep 2005 09:52 GMT
hello

i wrote a simple method to change row and column inclusive the
metainformation if you like.
for my purpose my result dataset has always only one row... so you have to
rewrite my method if you like with more then 1 row. but with 1 row it works
great!
public DataSet roatate(DataSet ds)
        {
            int rowCount = ds.Tables[0].Rows.Count;
            int colCount = ds.Tables[0].Columns.Count;

            ArrayList dataArray = new ArrayList();
            ArrayList columnNameArray = new ArrayList();
           
            object[] obj = ds.Tables[0].Rows[0].ItemArray;
            //save values from dataset
            for(int j =0; j<colCount;j++)
            {
                dataArray.Add(obj[j].ToString());
                columnNameArray.Add(ds.Tables[0].Columns[j].ColumnName);
            }
       
            DataRow myDataRow;
            DataTable myDataTable = new DataTable();
           
            //column for the meatainformation
            myDataTable.Columns.Add();
            myDataTable.Columns[0].ColumnName = "Bezeichnung";
   
            for(int i =0; i<colCount;i++)
            {
                //for each column make a new Row
                myDataRow = myDataTable.NewRow();   
                                           
                object[] objectArray = new object[rowCount+1];
                for(int j = 0;j<rowCount;j++)
                {
                    //for each row create a new column
                    if(i < rowCount)
                    {
                        myDataTable.Columns.Add();
                        myDataTable.Columns[i+1].ColumnName = "Wert";
                    }
                    objectArray[0] = columnNameArray[i].ToString();
                    objectArray[j+1] = dataArray[i].ToString();
                }
                myDataRow.ItemArray = objectArray;
                myDataTable.Rows.Add(myDataRow);
            }
            DataSet myDataSet = new DataSet();
            myDataSet.Tables.Add(myDataTable);
            return myDataSet;
        }

hth
thomas

>hi everyone
>
[quoted text clipped - 14 lines]
>
>Joey

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.