Link to home
Start Free TrialLog in
Avatar of JasonDecker
JasonDeckerFlag for United States of America

asked on

ASP.NET 1.1 C#: Cannot get value of a cell in datagrid when autogeneratecolumns="false"


I have a web app built in C# (.NET 1.1) and I need some help with one of my datagrids.

Here is the aspx code:
---------------------------------------------------------------------------------------------
<asp:datagrid autogeneratecolumns="False" id="dgRecentSearches" runat="server" allowsorting="True" borderstyle="None" borderwidth="0" gridlines="None" showheader="True" width="554">
                                                            <selecteditemstyle borderwidth="0px" borderstyle="None" backcolor="Gold"></selecteditemstyle>
                                                            <alternatingitemstyle borderwidth="0px" borderstyle="None" backcolor="White"></alternatingitemstyle>
                                                            <itemstyle borderwidth="0px" borderstyle="None" backcolor="White"></itemstyle>
                                                            <headerstyle backcolor="White" cssclass="static"></headerstyle>
                                                            <columns>
                                                                  <asp:editcommandcolumn visible="False" buttontype="LinkButton" updatetext="Update" canceltext="Cancel" edittext="Edit"></asp:editcommandcolumn>
                                                                  <asp:templatecolumn>
                                                                        <itemtemplate>
                                                                              <asp:checkbox id="chkDelete" runat="server" enabled="True" autopostback="True"></asp:checkbox>
                                                                        </itemtemplate>
                                                                  </asp:templatecolumn>
                                                                  <asp:templatecolumn headertext="ID" sortexpression='ID' visible="True">
                                                                        <itemtemplate>
                                                                              <asp:label id="lblRowID" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.ID")%>' />
                                                                        </itemtemplate>
                                                                  </asp:templatecolumn>            
                                                                  <asp:templatecolumn headerimageurl="/yellow_pages/graphics/icon_add.gif" sortexpression='DateSearched'>
                                                                        <itemtemplate>
                                                                              <asp:label id="Label8" style="text-align:left" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.DateSearched") %>' />
                                                                        </itemtemplate>
                                                                  </asp:templatecolumn>
                                                                  <asp:templatecolumn headerimageurl="/yellow_pages/graphics/icon_cat.gif" sortexpression='SearchTerm'>
                                                                        <itemtemplate>
                                                                              <asp:label id="Label9" style="text-align:left" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.SearchTerm") %>' />
                                                                        </itemtemplate>
                                                                  </asp:templatecolumn>
                                                                  <asp:templatecolumn headerimageurl="/yellow_pages/graphics/icon_loc.gif" sortexpression='SearchLocation'>
                                                                        <itemtemplate>
                                                                              <asp:label id="Label1" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.SearchLocation") %>' />
                                                                        </itemtemplate>
                                                                  </asp:templatecolumn>            
                                                            </columns>
                                                      </asp:datagrid>

---------------------------------------------------------------------------------------------

I have an imagebutton at the top of the page that will check to see if any of the checkboxes in the datagrid are checked or not. Whichever boxes are checked, I need to gather the ID from the ID column. For some reason, I can not get it. I am assuming that I can not get any values from the cells because the grid is set to autogetneratecolumns="false". But I am sort of a newb, so please bear with me.

Here is the aspx.cs code for the linkbutton click event:
---------------------------------------------------------------------------------------------

private void btnDeleteTop_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                  DataGridItem dgItem = null;
                  
                  CheckBox chkBox = null;
                  string strQString = "?delete=yes&";


                  for (int i=0; i < dgRecentSearches.Items.Count; i++)
                  {
                        dgItem = dgRecentSearches.Items[i];
                        string strChkID = "chkDelete";
                        chkBox = (CheckBox)dgItem.FindControl(strChkID);
                        if (chkBox.Checked)
                        {            
                              strQString += "chk=" + dgItem.Cells[0].Text.ToString() + "&";
                        }
                  }
                  strQString = strQString.Remove((strQString.Length - 1),1);
                  
                  //we won't redirect if we haven't selected any records
                  if(strQString.ToString() != "?delete=yes")
                        Response.Redirect("/yellow_pages/adapter_test.aspx"+strQString);
            }

---------------------------------------------------------------------------------------------

I have tried dgItem.Cells[1].Text.ToString() without success.

I would just use the dgItem.ItemIndex property to figure out which rows to remove from the grid, but my grid is sorted bidirectionally. On top of that, the datasource is an arraylist, so updating a dataview or datatable is not as simple as you might think.

I am happy with everything I have except for the fact that I can not figure out a way to get the ID of the row that I want to delete.

Keep in mind that the datasource is a cookie. The name value pairs in the cookie are parsed into individual objects, and then passed into the arraylist. The arraylist is then bound to the grid.

How can I get the value in the ID column of the row where the checkbox is checked?

Many thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of koolkraft
koolkraft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JasonDecker

ASKER

'System.Web.UI.WebControls.DataGridItem' does not contain a definition for 'DataKeys'

So I just used

dgRecentSearches.DataKeys[i].ToString()

and it worked! THANK YOU SO MUCH!
Avatar of koolkraft
koolkraft

Hi
Could you please grant me the points if it worked please.

Thanks
Done and thanks again!