Using Datagrid

  • Thread starter Thread starter Jeremy Ames
  • Start date Start date
J

Jeremy Ames

I am trying to use a datagrid to pull information from a database. I prefer
to use latebinding to this. Well, I finally got my information to populate
the datagrid, but the information came out in a table format. Is there a way
that I can make the datagrid use list box or some other kind of grid?
Perhaps I am using the wrong components. Someone please help.
 
I am sorry. I am using this on a web form. I have tried using the datalist
component and it tries to put the data into a table form as well. I would
assume that the repeater control will do the same. Is there an easy way to
get this info into a list box in a tabular format?

First, are you using a windows or web application?

For windows forms apps:
ListView might do what you want

For customizing the datagrid, see:
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp

For web apps:
Repeater control
DataList control
 
You can use a repeater to do this. Just put <select></select> around a
repeater, and repeat the <option> tags. A repeater does no extra
formatting- it just repeats what you have in the ItemTemplate, optionally
inserting what you have in the SeparatorTemplate (good place for a
<br><hr>,etc)

Like so, if you have the pubs database:

Put this in your aspx:
<select>
<asp:Repeater ID="ListData" Runat=server>
<ItemTemplate>
<option value='<%#DataBinder.Eval(Container.DataItem, "au_id")%>'>
<%# DataBinder.Eval(Container.DataItem, "au_lname") %>
</option>
</ItemTemplate>
</asp:Repeater>
</select>


Then bind in your code-behind, (something like this)

protected System.Web.UI.WebControls.Repeater ListData;

private void Page_Load(object sender, System.EventArgs e)
{
BindList();

}

private void BindList()
{
DataTable dt = new DataTable();
using(SqlConnection conn = new SqlConnection( "User
ID=sa;Password=;Initial Catalog=pubs;Data Source=."))
{
SqlCommand cmd = new SqlCommand("select * from authors", conn);
SqlDataAdapter da = new SqlDataAdapter( cmd);

conn.Open();
da.Fill( dt);

}
ListData.DataSource = dt;
ListData.DataBind();
}
 
Back
Top