[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 952
  • Last Modified:

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!
0
JasonDecker
Asked:
JasonDecker
  • 2
  • 2
1 Solution
 
koolkraftCommented:
Hi,

Consider changing your code to the following. Add a DataKeyField property to the declaration of the Datagrid and put the name of your primary key field there.

<asp:datagrid autogeneratecolumns="False" id="dgRecentSearches" runat="server" allowsorting="True" borderstyle="None" borderwidth="0" gridlines="None" showheader="True" width="554" DataKeyField="IDFIELD">

If you want to get values from a datagrid template column you should refer to them with the names of the controls so your btnDeleteTop_Click event handler should look like this :


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.Cells[1].FindControl(strChkID);
                    if (chkBox.Checked)
                    {          
                         strQString += "chk=" + dgItem.DataKeys[i].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);
          }
0
 
JasonDeckerAuthor Commented:
'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!
0
 
koolkraftCommented:
Hi
Could you please grant me the points if it worked please.

Thanks
0
 
JasonDeckerAuthor Commented:
Done and thanks again!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now