How to bind a gridview to a collection

liunx

Guest
Hi,

I am working in C# and sql server. I want to display the first letters of a field called "MerchantDisplayName" from

a table called "Merchant" in a database and the code is as follows :

have got the following files :

1)aspx file called loadgrid.aspx which contains the gridview control.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MerchantList_TestLoadSorted.ascx.cs"

Inherits="UserControls_MerchantList_TestLoadSorted" %>
<asp:GridView ID="TLCMerchant_Master" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="merchantalphabetically"/>
</Columns>
</asp:GridView>

2)The code behind page loadgrid.aspx.cs which contains the following code

public partial class UserControls_MerchantList_TestLoadSorted : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
Merchant_Master.DataSource = OnlineMallMerchants.FindMerchantAlphabetically();
DataBind();
}
}
}

3)i have a onlineMallMerchant.cs file which contains the following code

private OnlineMallMerchantsCollection merchantalphabetically;
public OnlineMallMerchantsCollection MerchantAlphabetically
{
get
{
merchantalphabetically = new

Merchants.DataBrokers.OnlineMallMerchantsDataBroker().FindMerchantAlphabetically();
return merchantalphabetically;
}
}

public static OnlineMallMerchantsCollection FindMerchantAlphabetically()
{
return new DataBrokers.OnlineMallMerchantsDataBroker().FindMerchantAlphabetically();
}


4) the OnlineMallMerchantCollection.cs file which contains the following code:

public class OnlineMallMerchantsCollection : BusinessBaseCollection

{

public OnlineMallMerchantsCollection()

{

}

public int Add(OnlineMallMerchants item)

{

return List.Add(item);

}

public void Remove(OnlineMallMerchants item)

{

List.Remove(item);

}

public OnlineMallMerchants this[int index]

{

get { return (OnlineMallMerchants) List[index]; }

}

}

5)the code for the method FindMerchantAlphabetically() is found on another .cs file called

OnlineMallMerchantsDataBroker.cs which has the following code :

public OnlineMallMerchantsCollection FindMerchantAlphabetically()
{
string sql = @"SELECT LEFT([MerchantDisplayName],1) AS [Alpha] FROM [Merchant] GROUP BY

LEFT([MerchantDisplayName],1) ORDER BY LEFT([MerchantDisplayName],1)";
return (OnlineMallMerchantsCollection) LoadList(string.Format(sql));
}

When the application is executed the number of rows returned is correct but the data displayed in each row is
"OnlineMallMerchantsCollection" instead of the starting letter.

What is wrong over here someone help????probably the datatextvalue is not set.

you have to set the datatextvalue to some column name within the db...

like,

Merchant_Master.DataSource = OnlineMallMerchants.FindMerchantAlphabetically();
Merchant_Master.DataTextField = "Column1"
Merchant_Master.DataValueField = "Column2"

the DataTextField is what will be shown on the screen.
The dataValueField is what you want to get for that text...(usually like, ID column)

-TakI am not using dataset but instead i am using attributes and i am linking the fields in a table to a property something like the following :

private string tlcmerchantdisplayname;
[MappedProperty("TLCMerchantDisplayName")]
public string TLCMerchantDisplayName
{
get{return tlcmerchantdisplayname;}
set { tlcmerchantdisplayname = value; }
}

where the [MappedProperty"TLCMerchantDisplayName")]

is the column name in the table and i am linking it to a property.

So in the datagrid after i bind the grid to a datasource i assign the Datafield value of the datagrid to the property name here is it
<asp:GridView ID="TLCMerchant_Master" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="TLCMerchantDisplayName"/>
</Columns>
</asp:GridView>

when i write the sql query as

"select TLCMerchantDisplayName from merchant"

the grid display data perfectly but when the query is changed to

"select left(TLCMerchantDisplayName,1) from merchant"

i dont know how to link the collection to the property

Do u get me takkie?

Any help appreciated
 
Back
Top