How to get selected cell value?

L

Lupina

Hi
I am trying to get value of certain cell, after I selected it (in datagrid).

private void UsersDataGrid_SelectedIndexChanged(object sender,
System.EventArgs e)
{
LabelInfo.Text = UsersDataGrid.SelectedItem.Cells[1].Text;
}but LabelInfo is still empty . Why?
 
K

Ken Cox [Microsoft MVP]

Hi Lupina,

Did you remember to only fill the datagrid if it is not a postback? The code
below works for me. Let us know?

Ken
Microsoft MVP [ASP.NET]
Toronto


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace p4320workcs
{
/// <summary>
/// Summary description for dgselected.
/// </summary>
public class dgselected : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label LabelInfo;
protected System.Web.UI.WebControls.DataGrid UsersDataGrid;

private void Page_Load(object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
UsersDataGrid.DataSource=CreateDataSource();
UsersDataGrid.DataBind();
}
}

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.UsersDataGrid.SelectedIndexChanged += new
System.EventHandler(this.UsersDataGrid_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void UsersDataGrid_SelectedIndexChanged(object sender,
System.EventArgs e)
{
LabelInfo.Text=UsersDataGrid.SelectedItem.Cells[1].Text;

}
}
}

<asp:Label id="LabelInfo" runat="server"></asp:Label></P>
<asp:DataGrid id="UsersDataGrid" runat="server">
<Columns>
<asp:ButtonColumn Text="Select"
CommandName="Select"></asp:ButtonColumn>
<asp:BoundColumn DataField="StringValue"
HeaderText="StringValue"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top