• 0

[C# / ASP.Net] Duplicated Datagrid Bound Column


Question

Hey all,

Probably a easy answer to this but its gone over my head. i guess its because im just coding the data to be bound directly to the data grid and not enforcing it to the correct column.

what ive tried to do is bind data depending on a SQL where statement to a datagrid. this works fine, but when i put in boundcolumns to have distinct headers and link the data to those colums it does to copies of the data, on correctly in the column and the other next to it.

Code is as follows-

in the aspx side:

<asp:DropDownList id="UserList" runat="server" DataTextField="Username" DataValueField="UserID" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="UserList_SelectedIndexChanged">
		<asp:ListItem Value="0">- Please Select A User</asp:ListItem>
		</asp:DropDownList>
		<br/><br/>
		<asp:DataGrid id="GridData" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" BorderWidth="1" BorderColor="black">
			<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
			<EditItemStyle BackColor="#2461BF" />
			<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
			<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
			<AlternatingItemStyle BackColor="White" />
			<ItemStyle BackColor="#EFF3FB" />
			<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

			<Columns>

			<asp:BoundColumn DataField="image" SortExpression="Image ID" HeaderText="Image ID" >
				<ItemStyle HorizontalAlign="Center" />
			</asp:BoundColumn>

			<asp:BoundColumn DataField="date_labelled" SortExpression="Date Labelled" HeaderText="Date Labelled" >
				<ItemStyle HorizontalAlign="Left" />
			</asp:BoundColumn>

			<asp:BoundColumn DataField="label_given" SortExpression="Label Given" HeaderText="Label Given" >
				<ItemStyle HorizontalAlign="Left" />
			</asp:BoundColumn>			

			<asp:BoundColumn DataField="confidence" SortExpression="Self Consistency" HeaderText="Self Consistency" >
				<ItemStyle HorizontalAlign="Left" />
			</asp:BoundColumn>



		</Columns>

		</asp:DataGrid>

and in the c# side:

public partial class Default3 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
	{
		if( ! IsPostBack)
		{
			string connection = WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;

		SqlConnection conn = new SqlConnection(connection);

		conn.Open();

		SqlCommand command = new SqlCommand("SELECT first_name, user_ID FROM users", conn);
		SqlDataAdapter adapter = new SqlDataAdapter(command);
		DataSet data = new DataSet(); adapter.Fill(data);

		UserList.DataSource = data;
		UserList.DataValueField = "user_ID";
		UserList.DataTextField = "first_name";
		UserList.DataBind();

		conn.Close();
		}
	}

protected void UserList_SelectedIndexChanged(Object sender, EventArgs e)
{
if(UserList.SelectedValue != "")
   {
	   // putting connection code here for tutorial
	   string connection = WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
	   SqlConnection conn = new SqlConnection(connection);
	   SqlCommand cmd = new SqlCommand("SELECT image, date_labelled, label_given, confidence FROM labelled_specimens WHERE taxonomist = @UserID", conn);
	   cmd.Parameters.AddWithValue("@UserID",UserList.SelectedValue);

	   conn.Open();
	   SqlDataReader reader = cmd.ExecuteReader();
	   GridData.DataSource = reader;

	   GridData.DataBind();


	   reader.Close();
	   conn.Close();
	}

if more code is required just let me know. im sure its something simple that ive overlooked.... but after a while it cant make head or tail of it.

thanks again!

Edited by Cheungo

3 answers to this question

Recommended Posts

  • 0

The reason columns are being duplicated is the AutoGenerateColumns property of the DataGrid. The default value for the property is true. So just add this attribute to asp:DataGrid: AutoGenerateColumns="False".

Second thing I noticed is SortExpression. It should reflect the column names.

  • 0
  amrinders87 said:
The reason columns are being duplicated is the AutoGenerateColumns property of the DataGrid. The default value for the property is true. So just add this attribute to asp:DataGrid: AutoGenerateColumns="False".

Second thing I noticed is SortExpression. It should reflect the column names.

haha!! thanks very much! i figured it would be something small... :s you have no idea how much that helps!!!!

ah oks. jut a quick question, what exactly does the sort expression do? just automatically arrange that colum via the vaule inside the speech marks?

  • 0
  Cheungo said:
haha!! thanks very much! i figured it would be something small... :s you have no idea how much that helps!!!!

ah oks. jut a quick question, what exactly does the sort expression do? just automatically arrange that colum via the vaule inside the speech marks?

Glad I could help you out.

Sort expression is used for sorting. In your case it really won't matter since they way you are databinding. If you want to add more features such as sorting, paging, I suggest you look into using the GridView control.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.