1

I'm creating this thread as a followed up from my previous one here.

I'm trying to create a gridview filled some data from my database. However, as you can see from the bottom 3 boundfield, i have prevented them from appearing on my webapp.

 <Columns>
    <asp:BoundField DataField="memberreportid" HeaderText="property" SortExpression="false"/>
    <asp:BoundField DataField="typeofcrime" HeaderText="property" SortExpression="false" />
    <asp:BoundField DataField="crdatetime" HeaderText="property" SortExpression="false" />
    <asp:BoundField DataField="address" HeaderText="property" SortExpression="false" />
    <asp:BoundField DataField="detail" HeaderText="property" SortExpression="false"/>
    <asp:BoundField DataField="incidentdate" HeaderText="property" SortExpression="false" />
    <asp:BoundField DataField="incidenttime" HeaderText="victim" SortExpression="false"/>
    <asp:BoundField DataField="property" HeaderText="suspect" SortExpression="false" Visible="false" />
    <asp:BoundField DataField="victim" HeaderText="suspect" SortExpression="false" Visible="false" />
    <asp:BoundField DataField="suspect" HeaderText="suspect" SortExpression="false" Visible="false" />
  </Columns>

I'm trying to display them out onto the label despite them not being able to see physically via the webpage. I used this method to display them out when the select button is being clicked ( I have set "AutoGenerateSelectColumn" to true)

protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbmemberreportid.Text = GWCase.SelectedRow.Cells[1].Text;
        lblproperty.Text = GWCase.SelectedRow.Cells[8].Text; 
        lblvictim.Text = GWCase.SelectedRow.Cells[9].Text;
        lblsuspect.Text = GWCase.SelectedRow.Cells[10].Text;
    }

Unfortunately, i'm only able to display out the memberreportID onto the label but not the other 3 attirbute which i believe it doesn't because i hid it physically. Is there any other method apart from the one i attempted to display out the specific value when being selected in the gridview?

UPDATED

Page_load binding

 protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoadGrid();
        }


    }

    private void LoadGrid()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
        conn.Open();

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("SELECT memberreportid, typeofcrime, crdatetime, address, detail, incidentdate, incidenttime, property, victim, suspect, detail, suspectdetail, propertydetail from memberreport", conn);
        da.Fill(ds);

        GWCase.DataSource = ds.Copy();
        GWCase.DataBind();

        conn.Close();

        ddlpid1.Visible = false;
        ddlpid2.Visible = false;
        ddlpid3.Visible = false;
        ddlpid4.Visible = false;
        ddlpid5.Visible = false;
        ddlpid6.Visible = false;
        ddlpid7.Visible = false;
        ddlpid8.Visible = false;
        ddlpid9.Visible = false;
        ddlpid10.Visible = false;
    }

Trying to read the hidden field

protected void GWCase_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadGrid();
        GWCase.PageIndex = e.NewPageIndex;
        GWCase.DataBind();
    }
Community
  • 1
  • 1
Bryan
  • 8,488
  • 14
  • 52
  • 78
  • just a note: `SortExpression` is not a boolean and therefore setting them to `false` on every column does not make any sense. It's meant to be a string that you can use to define the way the table sorts. Have a look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sortexpression.aspx – Bazzz Aug 01 '13 at 06:26
  • you're right. haha. however, this is not my main concern at the moment though. regards. – Bryan Aug 01 '13 at 06:32

3 Answers3

1

Well you can take a different approach and place the value in a hidden field:

<Columns>
    ...
    <asp:TemplateField>
        <ItemTemplate>
            <asp:HiddenField ID="hdfSuspect" runat="server" Value='<%# Eval("suspect") %>' />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

Then you can access it by doing the following:

protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
    ...
    lblsuspect.Text = ((HiddenField) GWCase.SelectedRow.FindControl("hdfSuspect")).Value;
}
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • yeah. i deleted the previous comment cause i actually left that out. but i soon got this error `Object reference not set to an instance of an object.` My database column isn't null but it didnt managed to get the database value into the textbox. I have checked that the ID and the findcontrol("ID") matches as well. What could still be the other problem ? – Bryan Aug 01 '13 at 06:08
  • @TeoChuenWeiBryan can you please include your `GridView` markup as well. Also at what point do you get the `Object reference not set to an instance of an objec.` exception ? What line is throwing it ? When you try to access the HiddenField ? Or when you're binding ? – Dimitar Dimitrov Aug 01 '13 at 06:19
  • It happens when i'm trying to access the details value. i have added the gridview binding and accessing in the question – Bryan Aug 01 '13 at 06:22
1

You need to use the property: DataKeyNames of GridView OR the FindControl() method when using TemplateFields

If using DataKeyNames:

Set datakeynames= "memberreportid" // use only Primary key. possible to use more than one field

You can access the same in SelectedIndexchanged event as: gridView1.DataKeys[gridView1.SelectedIndex].Value.ToString();

Refer this link for complete understanding on different ways to read hidden column values.

R.C
  • 10,417
  • 2
  • 35
  • 48
0

this is how you can display the value of grid in label

protected void  GWCase_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.DataItem != null)
        {
            Label5.Text  = e.Row.Cells[1].Text;

        }   
    }
Ahsan
  • 3
  • 7