Solved

(ASP.NET) How to bind data to a datagrid with template columns

Posted on 2002-05-01
7
700 Views
Last Modified: 2011-10-03
How do I bind data in a dataset (datable) to a datagrid with template columns?

Setup: I'm using a oledb connection and oledbdataAdapter. I have a dataset ds containing a datatable tbl, a datagrid dg containing three columns. Two of the three column are template columns, one contains a checkbox and one contains a textbox.

The DataTable contains three columns. two are string (varchar2) columns and the third is a 1-character string with a value of either 'T' or 'F' (represents true or false ).

I need to bind the 1-character string column to the checkbox/datagrid-column, one of the string columns to the textbox/datagrid-column.

After the dataAdapter fills the dataset and creates the datatable (oledbDataAdapter1.Fill (ds, "tbl") ), what do I need to do in order to bind the data to the datagrid?

Thanks.
0
Comment
Question by:eyebe97
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:naveenkohli
Comment Utility
You can bind your data grid to the DataAdapter or DataTable as follows..

protected void Page_Load(Object Src, EventArgs E)
   {
      // Create a connection to the "pubs" SQL database located on the
      // local computer.
      SqlConnection myConnection = new SqlConnection("server=localhost;" +
         "database=pubs;Trusted_Connection=Yes");
      // Connect to the SQL database using a SQL SELECT query to get all
      // the data from the "Authors" table.
      SqlDataAdapter myCommand = new SqlDataAdapter("SELECT " +
         " * FROM Authors", myConnection);
      // Create and fill a DataSet.
      DataSet ds = new DataSet();
      myCommand.Fill(ds);
      // Bind MyDataGrid to the DataSet. MyDataGrid is the ID for the
      // DataGrid control in the HTML section.
      DataView source = new DataView(ds.Tables[0]);
      MyDataGrid.DataSource = source ;
      MyDataGrid.DataBind();
   }

The main thing is to attach the data table to DataSource property of data grid.

Naveen
0
 

Author Comment

by:eyebe97
Comment Utility
How do I bind columns in the datatable to the template columns in the datagrid?
0
 
LVL 23

Accepted Solution

by:
naveenkohli earned 400 total points
Comment Utility
For binding to template columns this is how you can set up your data grid.

<asp:DataGrid Runat=server ......>
  <columns>
    <asp:boundColumnd DataField="Foo" HeaderText="Bound">
   
    <asp:TemplateColumn>
     <EditItemTemplate>
       <ItemTemplate>
         <asp:TextBox runat=server Text='<%#DataBinder.Eval(Container.DataItem, "fieldname") %>'
       </TemTemplate>
     </EditItemTemplate>
    </asp:TemplateColumn>
  </columns>

</asp:DataGrid>
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Expert Comment

by:naveenkohli
Comment Utility
If you want to set a column as read only, then you can set the ReadOnly=false for that DataGridcolumn. Otherwise set it to true.
For editing of columns you will need to add EditCommndColumn to grid that will handle your editing commands.

Take a look athe documentation of DataGrid. It has some nide and simple sample code.
0
 

Author Comment

by:eyebe97
Comment Utility
Is there a way to bind column in the datatable to the template columns in the datagrid programmatically ? I'm using the code behind feature.  All my code is in the page.aspx.cs (c#) file.
0
 
LVL 23

Expert Comment

by:naveenkohli
Comment Utility
Yeah you can do that...Infact thats the way I prefer.

Take a look at the following link. It has some descriptin on how to add columns in codebehind files.

http://www.pardesiservices.com/Softomatix/DGAutoGen.asp

If this does not help let me know. I can put togehter a simple example.

Naveen
0
 

Author Comment

by:eyebe97
Comment Utility
To bind data to columns when the datasource isn't available until runtime (meaning that I couldn't use the property builder for the datagrid), I found that I had to add the bound column code to the *.aspx page manually

<asp:BoundColumn HeaderText="" Datafield="">
</asp:BoundColumn>

***  IF the datasource were available at design time, I could have added the bound columns using the property builder ****

The template column could also be added to the aspx page manually or property builer could be used.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now