Two questions: datagrid with string[] and how to differentiate between columns

Discussion in 'ASP .Net Datagrid Control' started by Bob Weiner, May 4, 2005.

  1. Bob Weiner

    Bob Weiner Guest

    This is my first attempt at creating an ASP.Net app and first using the
    datagrid. Seems pretty nice but there are some peculiarities I can't figure
    out.

    Basically, I want a 3 column datagrid. The first column will list some
    files with the header text set to "File List." The second will be a button
    column with the word "View" to display the file in the browser as html. The
    third will also be a button column with the word "Download" to, you guessed
    it, download the selected file.

    My questions are:

    1. I would like to bind the datagrid directly to
    System.IO.Directory.GetFiles() which returns a string[]. The following
    works to a point:

    -------------------------
    <snip>
    <asp:DataGrid id="dgPickup" runat="server" AutoGenerateColumns="true"
    style="Z-INDEX: 110; LEFT: 72px; POSITION: absolute; TOP: 504px">
    <Columns>
    <asp:ButtonColumn Text="View" HeaderText="View"/>
    <asp:ButtonColumn Text="Download" HeaderText="Download"/>
    </Columns>
    </asp:DataGrid>
    </snip>
    --------------------------

    The file list is in the third column and labelled "Item;" I would like it
    to be the first column and labeled "File List." I have tried many ways to
    create a bound column but cannot figure out how to bind it to the string[].


    2. How do I determine what the user has selected? I can find the row
    using the DataGridCommandEventArgs.Item.ItemIndex, .Cells(), and/or
    ..UniqueID but I don't know how to determine which column within the row
    (view or download) that was selected.



    Last, this is just a newbie asp question, if the user clicks a view button,
    how do i redirect processing to another page passing it the name of the
    file?


    Thanks for any help,
    bob
    Bob Weiner, May 4, 2005
    #1
    1. Advertising

  2. Bob Weiner

    Elton W Guest

    Hi Bob,

    Let's discuss your questions one by one

    Q1:
    You can use
    datagrid.DataSource = System.IO.Directory.GetFiles(path);
    datagrid.DataBind();
    to bind data source.

    Q2:

    Normally the ItemCommand event handles any button click.
    If you assign CommandName for a ButtonColumn, e.g.
    <asp:ButtonColumn Text="View" HeaderText="View"
    CommandName="View" />
    then in ItemCommand you can find out clicked column by
    if (e.CommandName.Equals("View"))
    {
    // View Column clicked
    }

    Q3:
    You use e.Item.Cells[0].Text to get file name in the first
    column. And redirect to a new page:
    Response.Redirect("newpage.aspx?file=" + e.Item.Cells
    [0].Text);


    HTH

    Elton Wang


    >-----Original Message-----
    >This is my first attempt at creating an ASP.Net app and

    first using the
    >datagrid. Seems pretty nice but there are some

    peculiarities I can't figure
    >out.
    >
    >Basically, I want a 3 column datagrid. The first column

    will list some
    >files with the header text set to "File List." The

    second will be a button
    >column with the word "View" to display the file in the

    browser as html. The
    >third will also be a button column with the

    word "Download" to, you guessed
    >it, download the selected file.
    >
    >My questions are:
    >
    > 1. I would like to bind the datagrid directly to
    >System.IO.Directory.GetFiles() which returns a string[].

    The following
    >works to a point:
    >
    >-------------------------
    ><snip>
    > <asp:DataGrid id="dgPickup" runat="server"

    AutoGenerateColumns="true"
    > style="Z-INDEX: 110; LEFT: 72px; POSITION: absolute;

    TOP: 504px">
    > <Columns>
    > <asp:ButtonColumn Text="View" HeaderText="View"/>
    > <asp:ButtonColumn Text="Download"

    HeaderText="Download"/>
    > </Columns>
    > </asp:DataGrid>
    ></snip>
    >--------------------------
    >
    >The file list is in the third column and

    labelled "Item;" I would like it
    >to be the first column and labeled "File List." I have

    tried many ways to
    >create a bound column but cannot figure out how to bind

    it to the string[].
    >
    >
    > 2. How do I determine what the user has selected? I

    can find the row
    >using the DataGridCommandEventArgs.Item.ItemIndex, .Cells

    (), and/or
    >..UniqueID but I don't know how to determine which column

    within the row
    >(view or download) that was selected.
    >
    >
    >
    >Last, this is just a newbie asp question, if the user

    clicks a view button,
    >how do i redirect processing to another page passing it

    the name of the
    >file?
    >
    >
    >Thanks for any help,
    >bob
    >
    >
    >.
    >
    Elton W, May 6, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    13
    Views:
    767
    Richard Tobin
    Aug 11, 2004
  2. Replies:
    5
    Views:
    484
  3. Jo
    Replies:
    7
    Views:
    485
  4. Replies:
    1
    Views:
    149
    Peter Michaux
    Apr 27, 2007
  5. rhitam
    Replies:
    1
    Views:
    154
    David Mark
    May 13, 2009
Loading...

Share This Page