Link to home
Start Free TrialLog in
Avatar of raju1
raju1

asked on

how to retrieve value of a particular cell of datagrid

Dear Experts,
I want to retrieve cell value into a variable or text box. Pls help.
Avatar of mmarinov
mmarinov

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
Avatar of raju1

ASKER

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
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

Avatar of raju1

ASKER

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'.
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
Avatar of raju1

ASKER

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'.
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
Avatar of raju1

ASKER

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 '='.
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
Avatar of raju1

ASKER

<%@ 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>
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
Avatar of raju1

ASKER

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
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
Avatar of raju1

ASKER

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
raju1,


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

Regards!
B..M
mmarinov
Avatar of raju1

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

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