I have a gridview on the left which is basically a list of members. Now on the right it should display all the data associated with the member.
At first I'm supposed to select a member from the left gridview, then the gridview on the right should populate.
I've been trying hard to get the value of the selected element from the left but I'm not getting the right method.
Plus I need to display photo, Name , Age and all data in a different way, like an ID, so I'm getting second thoughts on whether GridView is the way to do it. And if not gridview, then what ?
If the left side just has to display one record i.e. person name, then you should use List View control
handling its selected item or selected index is straight forward
If your are particular to use a Gridview only on the left side, enable selection in Gridview and use Selected index Changed event or put button in all rows and use RowCommand to get the member from the Left Gridview. If you tell whether you are using BoundFileds
or TemplateField to display member name I can show some sample code
Kindly mark this post as "Answer", if it helped you.
I don't know why I can't bind the datasource to list view.
void BindData()
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select name FROM MInfo", con);
DataSet ds = new DataSet();
da.Fill(ds, "MemNames");
if (ds.Tables[0].Rows.Count > 0)
{
listView1.DataSource = ds.Tables["MemNames"];
listView1.DataBind();
//memListGrid.DataBind();
}
con.Close();
}
May I ask why do you use a ListView. For your Purpose ListBox is the apt control to use
Ok. If you want to populate ListView the code shoul be like this ,,
void BindData()
{
SqlConnection con = new SqlConnection(YourConnectionString");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select name FROM MInfo", con);
DataSet ds = new DataSet();
da.Fill(ds, "MemNames");
if (ds.Tables[0].Rows.Count > 0)
{
ListView1.DataSource = ds;
ListView1.DataBind();
// memListGrid.DataBind();
}
con.Close();
}
No need to DataBind the grid here, Also don't forget to configure the ListView at design time by puting item templates in it.
But If you want to select a member and use that value to change the gridview display only populating List view won't work..
You have to use a List Box. Populate it and Its SelectedIndexChanged event write code ..
Kindly mark this post as "Answer", if it helped you.
Thanks for the effort but DataValueField, DataTextField and DataBind() are showing errors!
Error 1 'System.Windows.Forms.ListBox' does not contain a definition for 'DataValueField' and no extension method 'DataValueField' accepting a first argument of type 'System.Windows.Forms.ListBox' could be found (are you missing a using directive or an assembly
reference?)
I have used these assemblies:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Only last three are added by me. Others we get by default in VWD 2010 express
Kindly mark this post as "Answer", if it helped you.
gideonn
Member
9 Points
21 Posts
Getting the value of the selected element in GridView
Mar 31, 2012 10:28 PM|LINK
I have a gridview on the left which is basically a list of members. Now on the right it should display all the data associated with the member.
At first I'm supposed to select a member from the left gridview, then the gridview on the right should populate.
I've been trying hard to get the value of the selected element from the left but I'm not getting the right method.
Plus I need to display photo, Name , Age and all data in a different way, like an ID, so I'm getting second thoughts on whether GridView is the way to do it. And if not gridview, then what ?
usman400
Contributor
3513 Points
721 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 03:18 AM|LINK
If the left side just has to display one record i.e. person name, then you should use List View control
handling its selected item or selected index is straight forward
basheerkal
Star
10672 Points
2426 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 03:35 AM|LINK
Hi
If your are particular to use a Gridview only on the left side, enable selection in Gridview and use Selected index Changed event or put button in all rows and use RowCommand to get the member from the Left Gridview. If you tell whether you are using BoundFileds or TemplateField to display member name I can show some sample code
(Talk less..Work more)
gideonn
Member
9 Points
21 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 06:24 AM|LINK
I don't know why I can't bind the datasource to list view.
void BindData() { SqlConnection con = new SqlConnection(connection); con.Open(); SqlDataAdapter da = new SqlDataAdapter("Select name FROM MInfo", con); DataSet ds = new DataSet(); da.Fill(ds, "MemNames"); if (ds.Tables[0].Rows.Count > 0) { listView1.DataSource = ds.Tables["MemNames"]; listView1.DataBind(); //memListGrid.DataBind(); } con.Close(); }Am I missing out something ?
basheerkal
Star
10672 Points
2426 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 07:04 AM|LINK
May I ask why do you use a ListView. For your Purpose ListBox is the apt control to use
Ok. If you want to populate ListView the code shoul be like this ,,
void BindData() { SqlConnection con = new SqlConnection(YourConnectionString"); con.Open(); SqlDataAdapter da = new SqlDataAdapter("Select name FROM MInfo", con); DataSet ds = new DataSet(); da.Fill(ds, "MemNames"); if (ds.Tables[0].Rows.Count > 0) { ListView1.DataSource = ds; ListView1.DataBind(); // memListGrid.DataBind(); } con.Close(); }No need to DataBind the grid here, Also don't forget to configure the ListView at design time by puting item templates in it.
But If you want to select a member and use that value to change the gridview display only populating List view won't work..
You have to use a List Box. Populate it and Its SelectedIndexChanged event write code ..
(Talk less..Work more)
gideonn
Member
9 Points
21 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 02:07 PM|LINK
How to bind the data to ListBox ?
listBox.DataSource ?
basheerkal
Star
10672 Points
2426 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 02:36 PM|LINK
Try this way
void FillListBox() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["YourString"].ConnectionString); con.Open(); SqlDataAdapter da = new SqlDataAdapter("Select field1 FROM YourTable", con); DataSet ds = new DataSet(); da.Fill(ds, "Field1"); if (ds.Tables[0].Rows.Count > 0) { ListBox1.DataSource = ds; ListBox1.DataValueField = "Field1"; ListBox1.DataTextField = "Field1"; ListBox1.DataBind(); } con.Close(); }And if youwant to update gridview display basedon that... use this sample code
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { string membername = ListBox1.SelectedValue.ToString().Trim(); SqlDataSource1.SelectCommand = "SELECT [idField], [field1], [field2] FROM [tester] WHERE Field1= '" + membername + "'"; GridView2.DataBind(); }(Talk less..Work more)
gideonn
Member
9 Points
21 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 02:47 PM|LINK
Thanks for the effort but DataValueField, DataTextField and DataBind() are showing errors!
Error 1 'System.Windows.Forms.ListBox' does not contain a definition for 'DataValueField' and no extension method 'DataValueField' accepting a first argument of type 'System.Windows.Forms.ListBox' could be found (are you missing a using directive or an assembly reference?)
I have used these assemblies:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
basheerkal
Star
10672 Points
2426 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 04:51 PM|LINK
These are the name spaces i used and no errors.
Only last three are added by me. Others we get by default in VWD 2010 express
(Talk less..Work more)
gideonn
Member
9 Points
21 Posts
Re: Getting the value of the selected element in GridView
Apr 01, 2012 05:44 PM|LINK
Isn't working for me. I'm using VS 2010 Pro.
I'm getting an error for System.Web.UI reference. Any explanation for that ? I'm designing a windows forms. Do I need Web UI ?