1
Answer

How to bind datagrid to a collection?

Jo Ju

Jo Ju

18y
4.1k
1

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????

Answers (1)