Solved

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

Posted on 2002-05-01
7
705 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: 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!

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 78
Multiflying 2 Input Text On a Table 7 36
Insert Button on a table 16 55
IIS components 2 66
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/…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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