How to get row data from DataGrid ASP:Button Click

Discussion in 'ASP .Net' started by DBLWizard, Apr 5, 2005.

  1. DBLWizard

    DBLWizard Guest

    Howdy All,

    I am trying to get the data from a row of a data grid in asp.net This
    is a data grid with five columns (two of the hidden) as follows:



    <asp:datagrid id="grdMatching" runat="server" Width="100%"
    Visible="False" Height="160px" AutoGenerateColumns="False"
    AllowSorting="True" onselectedindexchanged="GetRow">
    <Columns>
    <asp:ButtonColumn Text="Select" ButtonType="PushButton"
    CommandName="Select"></asp:ButtonColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    Name
    </HeaderTemplate>
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "FullName") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn >
    <HeaderTemplate>
    Matches
    </HeaderTemplate>
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Matches") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn Visible="False">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Name") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn Visible="False">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "GivenName") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>

    I am trying to use the ItemCommand event to get the data from the cells
    as shown in the MSDN help but everything it comes back blank. Can
    somebody tell me what I a am doing wrong? Here is the code I am using
    in the ItemCommand event:

    private void grdMatching_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string sConnection = "user
    id=user;password=password;database=dbname;server=servername;Connect
    Timeout=30";
    SqlConnection myConn = new SqlConnection(sConnection);
    SqlCommand myCommand;
    SqlDataAdapter myDA = new SqlDataAdapter();
    DataSet myDS;
    string sName = "";
    string sGivenName = "";
    string sSql = "";

    TableCell objCell = e.Item.Cells[3];
    sName = objCell.Text;
    objCell = e.Item.Cells[4];
    sGivenName = objCell.Text;

    if (sName.Length > 0 || sGivenName.Length > 0)
    {
    sSql = "Select top 100 InstrNum, FileType, FileDate, Book, Page,
    Series, Type, Description, Series, Name, GivenName, RTrim([Name]) +
    ISNULL(',' + RTrim([GivenName]), '') As [FullName] From NCLand Where
    [Name] = '" + sName + "' AND [GivenName] = '" + sGivenName + " Order by
    [Name], [GivenName]";
    myCommand = new SqlCommand(sSql, myConn);
    myDA = new SqlDataAdapter();
    myDA.SelectCommand = myCommand;
    myConn.Open();
    myDS = new DataSet();
    myDA.Fill(myDS, "SearchResults");
    grdItems.DataSource = myDS.Tables["SearchResults"].DefaultView;
    grdItems.DataBind();
    grdItems.Visible = true;
    }
    }



    This event fires but both cells come back with empty strings. How can
    I get the data out of the cells?

    Thanks

    dbl
    DBLWizard, Apr 5, 2005
    #1
    1. Advertising

  2. DBLWizard

    Teemu Keiski Guest

    Try placing Labels into the hidden columns and bind the values to them. When
    checking for values, locate controls using FindControl method
    (e.Item.FindControl...) and get the Text value from Label's Text property.

    --
    Teemu Keiski
    ASP.NET MVP, Finland


    "DBLWizard" <> wrote in message
    news:...
    > Howdy All,
    >
    > I am trying to get the data from a row of a data grid in asp.net This
    > is a data grid with five columns (two of the hidden) as follows:
    >
    >
    >
    > <asp:datagrid id="grdMatching" runat="server" Width="100%"
    > Visible="False" Height="160px" AutoGenerateColumns="False"
    > AllowSorting="True" onselectedindexchanged="GetRow">
    > <Columns>
    > <asp:ButtonColumn Text="Select" ButtonType="PushButton"
    > CommandName="Select"></asp:ButtonColumn>
    > <asp:TemplateColumn>
    > <HeaderTemplate>
    > Name
    > </HeaderTemplate>
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "FullName") %>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn >
    > <HeaderTemplate>
    > Matches
    > </HeaderTemplate>
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Matches") %>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn Visible="False">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Name") %>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn Visible="False">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "GivenName") %>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:datagrid>
    >
    > I am trying to use the ItemCommand event to get the data from the cells
    > as shown in the MSDN help but everything it comes back blank. Can
    > somebody tell me what I a am doing wrong? Here is the code I am using
    > in the ItemCommand event:
    >
    > private void grdMatching_ItemCommand(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > string sConnection = "user
    > id=user;password=password;database=dbname;server=servername;Connect
    > Timeout=30";
    > SqlConnection myConn = new SqlConnection(sConnection);
    > SqlCommand myCommand;
    > SqlDataAdapter myDA = new SqlDataAdapter();
    > DataSet myDS;
    > string sName = "";
    > string sGivenName = "";
    > string sSql = "";
    >
    > TableCell objCell = e.Item.Cells[3];
    > sName = objCell.Text;
    > objCell = e.Item.Cells[4];
    > sGivenName = objCell.Text;
    >
    > if (sName.Length > 0 || sGivenName.Length > 0)
    > {
    > sSql = "Select top 100 InstrNum, FileType, FileDate, Book, Page,
    > Series, Type, Description, Series, Name, GivenName, RTrim([Name]) +
    > ISNULL(',' + RTrim([GivenName]), '') As [FullName] From NCLand Where
    > [Name] = '" + sName + "' AND [GivenName] = '" + sGivenName + " Order by
    > [Name], [GivenName]";
    > myCommand = new SqlCommand(sSql, myConn);
    > myDA = new SqlDataAdapter();
    > myDA.SelectCommand = myCommand;
    > myConn.Open();
    > myDS = new DataSet();
    > myDA.Fill(myDS, "SearchResults");
    > grdItems.DataSource = myDS.Tables["SearchResults"].DefaultView;
    > grdItems.DataBind();
    > grdItems.Visible = true;
    > }
    > }
    >
    >
    >
    > This event fires but both cells come back with empty strings. How can
    > I get the data out of the cells?
    >
    > Thanks
    >
    > dbl
    >
    Teemu Keiski, Apr 5, 2005
    #2
    1. Advertising

  3. Columns with Visible=false are not rendered to client. That's why you are
    getting back empty strings. You should hide columns with css style
    display:none.

    Eliyahu

    "DBLWizard" <> wrote in message
    news:...
    > Howdy All,
    >
    > I am trying to get the data from a row of a data grid in asp.net This
    > is a data grid with five columns (two of the hidden) as follows:
    >
    >
    >
    > <asp:datagrid id="grdMatching" runat="server" Width="100%"
    > Visible="False" Height="160px" AutoGenerateColumns="False"
    > AllowSorting="True" onselectedindexchanged="GetRow">
    > <Columns>
    > <asp:ButtonColumn Text="Select" ButtonType="PushButton"
    > CommandName="Select"></asp:ButtonColumn>
    > <asp:TemplateColumn>
    > <HeaderTemplate>
    > Name
    > </HeaderTemplate>
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "FullName") %>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn >
    > <HeaderTemplate>
    > Matches
    > </HeaderTemplate>
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Matches") %>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn Visible="False">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "Name") %>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn Visible="False">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container.DataItem, "GivenName") %>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:datagrid>
    >
    > I am trying to use the ItemCommand event to get the data from the cells
    > as shown in the MSDN help but everything it comes back blank. Can
    > somebody tell me what I a am doing wrong? Here is the code I am using
    > in the ItemCommand event:
    >
    > private void grdMatching_ItemCommand(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > string sConnection = "user
    > id=user;password=password;database=dbname;server=servername;Connect
    > Timeout=30";
    > SqlConnection myConn = new SqlConnection(sConnection);
    > SqlCommand myCommand;
    > SqlDataAdapter myDA = new SqlDataAdapter();
    > DataSet myDS;
    > string sName = "";
    > string sGivenName = "";
    > string sSql = "";
    >
    > TableCell objCell = e.Item.Cells[3];
    > sName = objCell.Text;
    > objCell = e.Item.Cells[4];
    > sGivenName = objCell.Text;
    >
    > if (sName.Length > 0 || sGivenName.Length > 0)
    > {
    > sSql = "Select top 100 InstrNum, FileType, FileDate, Book, Page,
    > Series, Type, Description, Series, Name, GivenName, RTrim([Name]) +
    > ISNULL(',' + RTrim([GivenName]), '') As [FullName] From NCLand Where
    > [Name] = '" + sName + "' AND [GivenName] = '" + sGivenName + " Order by
    > [Name], [GivenName]";
    > myCommand = new SqlCommand(sSql, myConn);
    > myDA = new SqlDataAdapter();
    > myDA.SelectCommand = myCommand;
    > myConn.Open();
    > myDS = new DataSet();
    > myDA.Fill(myDS, "SearchResults");
    > grdItems.DataSource = myDS.Tables["SearchResults"].DefaultView;
    > grdItems.DataBind();
    > grdItems.Visible = true;
    > }
    > }
    >
    >
    >
    > This event fires but both cells come back with empty strings. How can
    > I get the data out of the cells?
    >
    > Thanks
    >
    > dbl
    >
    Eliyahu Goldin, Apr 5, 2005
    #3
  4. DBLWizard

    DBLWizard Guest

    I saw that, have fixed it, and verified that the table cells are
    actually there in the source but they code still returnes blanks. I
    added code to check all the cells in the row and they all come back
    blank!!!!

    Any thoughts?

    Thanks

    dbl
    DBLWizard, Apr 5, 2005
    #4
  5. DBLWizard

    DBLWizard Guest

    As I am reading more of the documentation it says that for "bound"
    columns the values are in the Cell.Text. But I am using Template
    Columns ... but it doesn't say how to get to the data for template
    columns!!!! Is there a better way to do this?

    Thanks

    dbl
    DBLWizard, Apr 5, 2005
    #5
    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. Purvi T
    Replies:
    0
    Views:
    821
    Purvi T
    Oct 19, 2004
  2. Amy
    Replies:
    0
    Views:
    961
  3. sauce
    Replies:
    0
    Views:
    225
    sauce
    Apr 12, 2005
  4. D
    Replies:
    0
    Views:
    198
  5. sophistLady via .NET 247

    get datagrid row number after click on hyperlink

    sophistLady via .NET 247, Mar 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    332
    quaester
    Mar 9, 2005
Loading...

Share This Page