Solved

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

Posted on 2002-05-01
7
704 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
ID: 6984801
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
ID: 6984804
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
ID: 6984816
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:naveenkohli
ID: 6984819
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
ID: 6984821
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
ID: 6984829
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
ID: 6990764
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBSCRIPT string remove all characters to the right after extension 4 71
is this a cms? 8 69
Html split(text) 2 26
JS to redirect to prev page 8 16
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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