Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 712
  • Last Modified:

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

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
eyebe97
Asked:
eyebe97
  • 4
  • 3
1 Solution
 
naveenkohliCommented:
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
 
eyebe97Author Commented:
How do I bind columns in the datatable to the template columns in the datagrid?
0
 
naveenkohliCommented:
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!

 
naveenkohliCommented:
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
 
eyebe97Author Commented:
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
 
naveenkohliCommented:
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
 
eyebe97Author Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now