• 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.
  • Posts

    • RIP Hotlips..... IMHO, her best scenes were the few where she dared to let her REAL feminine side show through from underneath all that crappy, worn-torn soldier facade that she had to keep up with...she instantly lit up every room or situation where she was featured !
    • Helium Converter 3.3.69.0 by Razvan Serea Helium Converter is a free Windows utility for converting audio files between formats such as MP3, FLAC, AAC, WMA, OGG, and WAV. It supports batch conversion, preserves or updates tag information, and offers features like volume normalization. With a simple interface, it's ideal for users who need to convert large music libraries quickly and efficiently while retaining metadata. Helium Converter key features: Supports file formats: MP3, MP4, FLAC, AAC, M4A, WMA, WAV, OGG, OPUS, APE.... Batch conversion for large music libraries Preserves and edits metadata (ID3, Vorbis Comments, etc.) Volume normalization to equalize loudness Album art extraction and embedding Drag-and-drop interface for quick file selection Adjustable encoding parameters (bitrate, sample rate, channels) Uses internal codecs for consistent performance Supports CUE sheets for split track conversion File renaming based on tags during export Unicode support for international file and tag names Logging of conversion processes for troubleshooting Multi-core CPU support for faster conversions Download: Helium Converter 3.3.69.0 | 25.1 MB (Freeware) Links: Helium Converter Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Since it's been quite a while since the last episode aired, would it be fair (or cruel) to refer to Peggy as a MILF, or even a SMILF ?  HAHAHAHAHAHA
    • Their computers are not gonna stop working in October
    • Crowdstrike the same service provider that caused millions of in damages? I hate disliking a company for a singular failure but they really screwed up.
  • Recent Achievements

    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      172
    2. 2
      snowy owl
      122
    3. 3
      ATLien_0
      122
    4. 4
      Xenon
      116
    5. 5
      +Edouard
      93
  • Tell a friend

    Love Neowin? Tell a friend!