Solved

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

Posted on 2002-05-01
7
707 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

615 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