Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get value from DataGrid/DataSet

Status
Not open for further replies.

ROGERDODGE

IS-IT--Management
Aug 13, 2003
40
GB
Hi
I'm fairly new to c# and .Net coming from Visual Fox background.

I have a comboBox which on selection fills a DataSet to which I have bound a DataGrid by passing a value.
As I move up and down this DataGrid I want to fire an event to fill another Dataset by passing a unique value from the data row. I have been struggling with this for a couple of days now to derive the value of the column.

Do I find it from the grid or the DataSet.

Please someone point me in the right direction

Roger
 
The DataGrid has a property DataKeyValue. You can assign it a boundcolumn from your grid or a datacolumn from your datatable. To access it use YourGrid.DataKeys[e.Item.ItemIndex].
Marty
 
Hi Marty,

thanks for response

ermmmm.. my DataGrid only has DataMember and DataSource properties beginning with 'D'

Just to clarify I am using MS Visual C# .NET

Roger
 
example
codebehind
Code:
	public class TestDataGrid : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack) 
			{
				DataGrid1.DataSource = GetData();
				DataGrid1.DataKeyField = GetData().Columns["RateCapId"].ColumnName;
				DataGrid1.DataBind();

			}
			// Put user code to initialize the page here
		}

		#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.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
		public static DataTable GetData()
		{
			DataSet ds = new DataSet();
			ds = DAL.GetRefRateCap();
			return ds.Tables[0];
		}

		public void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "Select":
					Label1.Text = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
					break;
				default:
					break;
			}
		}
		
	}
aspx
Code:
	<body MS_POSITIONING="GridLayout">
		<h3>Test Data Grid</h3>
		<form id="Form1" method="post" runat="server">
			<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="False">
				<Columns>
					<asp:ButtonColumn Text="Select" HeaderText="Select" CommandName="Select"></asp:ButtonColumn>
				</Columns>
			</asp:DataGrid>
		</form>
		<br>
		<asp:Label id="Label1" runat="server">Label</asp:Label>
	</body>
if you edit your aspx in HTML view you hit the spacebar after <asp:DataGrid you will see alot of properties and events.
Marty
 
Thanks for the detail - I'll try that later

Roger
 
Hi All;

I may be wrong, but properties like DataKeyValue seem to exist only for the Web version of DataGrid. If you are using
Winforms you are out of luck, or will have to buy a third party control?!?

Just My 0.02$'s
MeonR
 
Hi Guys

Spot on - MeonR ! WinForms doesn't have that property.

I have approached the 'problem' from a different direction and instead of 'Cascading' the events I have the combo change populate a DataSet with two tables and define a one-to-many relationship between the tables. By data-binding the grids to the relevant bits in the DataSet, I get exactly what I wanted.

Got there in the end! I am finding the most difficult thing with .NET is to change the approach to a problem over other languages like VisualFox.

Stay C-Sharp!

Roger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top