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

x
?
Solved

how to retrieve value of a particular cell of datagrid

Posted on 2004-11-11
17
Medium Priority
?
991 Views
Last Modified: 2008-03-03
Dear Experts,
I want to retrieve cell value into a variable or text box. Pls help.
0
Comment
Question by:raju1
  • 9
  • 8
17 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12553388
Hi raju1,

datagrid.Items[index of the row].Cells[index of the column].Text <- this will return you the content in the cell if you have not controls there ( bound column for example )
if you have controls

TextBox tb = (TextBox)Datagrid.Items[index of the row].Cells[index of the column].FindControl("id of the control");
and then tb.Text

Regards!
B..M
mmarinov
0
 

Author Comment

by:raju1
ID: 12553928
Thanks for reply. I could not solve this problem. I have written the code in where i have got the errors. Pls note that the datagrid is user_info.

Sub DEDR_Delete(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
      Dim strSQL As String, strConnection As String
      Dim EditText As TextBox, iRow As Integer

      strConnection = ConfigurationSettings.AppSettings("connectionstring")
      Dim myCon As New SqlConnection(strConnection)
      myCon.Open()

      iRow = E.Item.ItemIndex
      EditText = user_info.Items[irow].ce   ' is not working
      strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0])

      Dim cmd As New SqlCommand(strSQL, myCon)
      cmd.ExecuteNonQuery()
      LoadMyCalendarData()
   End Sub
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12553985
try this

strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0].Text)
are there controls in the first column or it is bound column that is not visible to user ?

B..M
mmarinov

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:raju1
ID: 12554063
Yes, it is first column. I have done according to ur comment, but it is not working. It generates the following error message.

Value of type 'System.Web.UI.WebControls.TableCellCollection' cannot be converted to 'String'.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12554078
raju1,

use the code from my second post
you can direct cast the e.item.Cells(index)
aslo i've noticed that there is an error
use this

strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells(0).Text)

Regards!
B..M
mmarinov
0
 

Author Comment

by:raju1
ID: 12554121
Thanks for quick response.

yeah i have used as follows:

strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0].text)

It generates the following error:

Value of type 'System.Web.UI.WebControls.TableCellCollection' cannot be converted to 'String'.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12554141
raju1,
not this, but this
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells(0).text) <-- see here there are no [] brakets

Regards!
B..M
mmarinov
0
 

Author Comment

by:raju1
ID: 12554217
Now the coding is as follows:

Sub DEDR_Delete(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
      Dim strSQL As String, strConnection As String
      Dim EditText As TextBox, iRow As Integer

      strConnection = ConfigurationSettings.AppSettings("connectionstring")
      Dim myCon As New SqlConnection(strConnection)
      myCon.Open()

      'iRow = E.Item.ItemIndex
      'EditText = user_info.Items[irow].ce
      strSQL = "DELETE UserInfo WHERE UserID = " & CStr(E.Item.Cells(0).Text)

      Dim cmd As New SqlCommand(strSQL, myCon)
      cmd.ExecuteNonQuery()
      LoadMyCalendarData()
   End Sub


At this moment, after debugging the strSQL contains the following value:
"DELETE UserInfo WHERE UserID = "

i.e., CStr(E.Item.Cells(0).Text) can not contain value.

For that here is new error like
Incorrect syntax near '='.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12554260
raju1,

can you post the datagrid html
if the e.item.cells(0).text does not return anything, you have 2 scenarios:
1. there is nothing - you don't bind it
2. you use template column

Regards!
B..M
mmarinov
0
 

Author Comment

by:raju1
ID: 12554286
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="usercode.aspx.vb" Inherits="bismilla.usercode"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>usercode</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <FORM id="Form1" runat="server">
                  <asp:Label id="ErrorMessage" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"
                        runat="server"></asp:Label><BR>
                  <asp:LinkButton id="LinkButton1" onclick="DEDR_Add" runat="server" Text="Add new event">Add new user</asp:LinkButton><BR>
                  <asp:DataGrid id="user_info" runat="server" AutoGenerateColumns="false" width="100%" OnEditCommand="DEDR_Edit"
                        OnUpdateCommand="DEDR_Update" OnCancelCommand="DEDR_Cancel" OnDeleteCommand="DEDR_Delete">
                        <HeaderStyle ForeColor="White" BackColor="DodgerBlue" Font-Bold="true" />
                        <ItemStyle BackColor="White" />
                        <AlternatingItemStyle BackColor="Gainsboro" />
                        <Columns>
                              <asp:TemplateColumn HeaderText="User ID">
                                    <ItemTemplate>
                                          <%# DataBinder.Eval(Container.DataItem, "UserID") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:TextBox id="txtUserID" Size="10" Text='<%# Container.DataItem("UserID") %>' runat="server"/>
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn HeaderText="User Name">
                                    <ItemTemplate>
                                          <%# Container.DataItem("UserName") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:TextBox id="txtUserName" Size="40" Text='<%# Container.DataItem("UserName") %>' runat="server"/>
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn HeaderText="Password">
                                    <ItemTemplate>
                                          <%# Container.DataItem("Password") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:TextBox id="txtPassword" Size="10" Text='<%# Container.DataItem("Password") %>' runat="server"/>
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn HeaderText="User Level">
                                    <ItemTemplate>
                                          <%# Container.DataItem("UserLevel") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:TextBox id="txtUserLevel" Size="7" Text='<%# Container.DataItem("UserLevel") %>' runat="server"/>
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn HeaderText="Full Name">
                                    <ItemTemplate>
                                          <%# Container.DataItem("FullName") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:TextBox id="txtFullName" Size="40" Text='<%# Container.DataItem("FullName") %>' runat="server"/>
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                              <asp:TemplateColumn>
                                    <ItemTemplate>
                                          <asp:LinkButton CommandName="Edit" Text="Edit" runat="server" />
                                          <asp:LinkButton CommandName="Delete" Text="Delete" runat="server" />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                          <asp:LinkButton CommandName="Cancel" Text="Cancel" runat="server" />
                                          <asp:LinkButton CommandName="Update" Text="Update" runat="server" />
                                    </EditItemTemplate>
                              </asp:TemplateColumn>
                        </Columns>
                  </asp:DataGrid></FORM>
      </body>
</HTML>
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12554410
raju1,

so replace this line
strSQL = "DELETE UserInfo WHERE UserID = " & CStr(E.Item.Cells(0).Text)
with this one
strSQL = "DELETE UserInfo WHERE UserID = " & CType(E.Item.FindControl("txtUserID"), TextBox).Text

Regards!
B..M
mmarinov
0
 

Author Comment

by:raju1
ID: 12554452
New error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 123:      'iRow = E.Item.ItemIndex
Line 124:      'EditText = user_info.Items[irow].ce
Line 125:      strSQL = "DELETE UserInfo WHERE UserID = " & CType(E.Item.FindControl("txtUserID"), TextBox).Text
Line 126:
Line 127:      Dim cmd As New SqlCommand(strSQL, myCon)
 
Error in Line: 125
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12554505
raju1,

debug the program and check this collection before building the query
e.Item.Cells(0).Controls
how many controls are there ? is there any control that is textbox or label if there is any then its Text property will contain the userid value

Regards!
B..M
mmarinov
0
 

Author Comment

by:raju1
ID: 12554615
E.Item.Cells(0).Controls {System.Web.UI.ControlCollection} System.Web.UI.ControlCollection
Count                        1      Integer
IsReadOnly      False      Boolean
IsSynchronized      False      Boolean
Item      <cannot view indexed property>      System.Web.UI.Control
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12554667
raju1,


so check then what do you have in
e.Item.Cells(0).Controls(0)

Regards!
B..M
mmarinov
0
 

Author Comment

by:raju1
ID: 12554709
There are lot of parameters. What information do u need to fix the problem?

-      e.Item.Cells(0).Controls(0)      {System.Web.UI.DataBoundLiteralControl}      System.Web.UI.Control
-      [System.Web.UI.DataBoundLiteralControl]      {System.Web.UI.DataBoundLiteralControl}      System.Web.UI.DataBoundLiteralControl
+      BindingContainer      {System.Web.UI.WebControls.DataGridItem}      System.Web.UI.Control
      ClientID      "user_info__ctl3__ctl2"      String
+      Controls      {System.Web.UI.EmptyControlCollection}      System.Web.UI.ControlCollection
      EnableViewState      True      Boolean
      ID      Nothing      String
+      NamingContainer      {System.Web.UI.WebControls.DataGridItem}      System.Web.UI.Control
+      Page      {ASP.usercode_aspx}      System.Web.UI.Page
+      Parent      {System.Web.UI.WebControls.TableCell}      System.Web.UI.Control
      Site      Nothing      System.ComponentModel.ISite
      TemplateSourceDirectory      "/bismilla"      String
      Text      "
                                          975
                                    "      String
      UniqueID      "user_info:_ctl3:_ctl2"      String
      Visible      True      Boolean
+      BindingContainer      {System.Web.UI.WebControls.DataGridItem}      System.Web.UI.Control
      ClientID      "user_info__ctl3__ctl2"      String
-      Controls      {System.Web.UI.EmptyControlCollection}      System.Web.UI.ControlCollection
+      [System.Web.UI.EmptyControlCollection]      {System.Web.UI.EmptyControlCollection}      System.Web.UI.EmptyControlCollection
      Count      0      Integer
      IsReadOnly      False      Boolean
      IsSynchronized      False      Boolean
      Item      <cannot view indexed property>      System.Web.UI.Control
+      SyncRoot      {System.Web.UI.EmptyControlCollection}      Object
      EnableViewState      True      Boolean
      ID      Nothing      String
+      NamingContainer      {System.Web.UI.WebControls.DataGridItem}      System.Web.UI.Control
+      Page      {ASP.usercode_aspx}      System.Web.UI.Page
+      Parent      {System.Web.UI.WebControls.TableCell}      System.Web.UI.Control
      Site      Nothing      System.ComponentModel.ISite
      TemplateSourceDirectory      "/bismilla"      String
      UniqueID      "user_info:_ctl3:_ctl2"      String
      Visible      True      Boolean
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 150 total points
ID: 12554818
raju1,

i think the hole problem is cause by this use
<asp:TemplateColumn HeaderText="User ID">
                              <ItemTemplate>
                                   <%# DataBinder.Eval(Container.DataItem, "UserID") %>
                              </ItemTemplate>
                              <EditItemTemplate>
                                   <asp:TextBox id="txtUserID" Size="10" Text='<%# Container.DataItem("UserID") %>' runat="server"/>
                              </EditItemTemplate>
                         </asp:TemplateColumn>

try with these changes

<asp:TemplateColumn HeaderText="User ID">
                              <ItemTemplate>
                                   <asp:Label id="lblId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "UserID") %>' />
                              </ItemTemplate>
                              <EditItemTemplate>
                                   <asp:TextBox id="txtUserID" Size="10" Text='<%# Container.DataItem("UserID") %>' runat="server"/>
                              </EditItemTemplate>
                         </asp:TemplateColumn>

and

strSQL = "DELETE UserInfo WHERE UserID = " & CType(E.Item.FindControl("lblId"), Label).Text

Regards!
B..M
mmarinov
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question