.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How to bind GridView and DropDownList controls using DataTable object

Posted By: Syed Shakeer Hussain     Posted Date: June 17, 2010    Points: 2   Category :ADO.Net
In .net framework 2.0, you can load a DataTable in memory by consuming a data source using a DataReader. In this version you will find the flexibility in accessing the DataReader means write access code once and use again and again using DataReader. To see the functionality of DataTable consider an example that is created using VS2005.

View Complete Post

More Related Resource Links

dropdownlist controls in gridview issue

 the below code creates drop down each time i add a category to the database table. however, i don't wish to have drop down created each time - how can i have all my data in one drop down? thanks...  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CascadingDropDownTestConnectionString %>"  SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Category]">  </asp:SqlDataSource>    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"  DataSourceID="SqlDataSource1">  <Columns>  <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"  ReadOnly="True" SortExpression="CategoryID" />  <asp:TemplateField HeaderText="Categories">  <ItemTemplate>  <asp:DropDownList ID="ddlCategories" AutoPostBack="true" DataTextField="CategoryName"  DataValueField="CategoryID" DataSourceID="SqlDataSource1" runat="server" AppendDataBoundItems="true"  SelectedValue='<%# Bind("CategoryID") %>' />  &

read delimited files into a datatable and bind to a gridview

I have a delimted file , like a csv file but the delimters are # and not ,I assume I would use SqlConnection and SqlDataAdapterSo for a csv file what is the sequence to read in for example c:\data\demo.csvWhat would the connection string be.Also can you specifiy a different delimiter.The code below is what I assume it would possibly look likea although its only a frst guess. ThanksJim Dim connectionString As String = "Data Source=localhost;Initial Catalog=master;Integrated Security=true;" Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Northwind_ConnectionString").ConnectionString) Dim str As String = "SELECT * FROM Employees" Dim cmd As New SqlCommand(str, con) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable() da.Fill(dt) GridView1.DataSource = dt GridView1.DataBind()

Bind a GridView to a DataTable (in Viewstate) with full in-line CRUD


Hello fellow devs!

I need a bit of help with a scenario. I am working on a web application that requires huge amounts of data to be Added, Deleted, and Updated. The data entry forms are divided into logical groups through Multiviews. All the information is saved when the mighty Finish button is pressed. The current setup (previous developer) does not allow me to use transactions. Therefore, if I am to save a new Courier to the database, I need to add his/her Distance and Rate info. In addition, I need to add his/her Banned Areas info (Area Name, Post Code).

This is where it gets interesting. Obviously, the DistamceAndRate table and the BannedArea table in my SQL Server will have the CourierID as a foreign key. Since I'm going to save the Courier as well as the Rates and Areas info in one go, I cannot have the newly created CourierID before. Therefore, I cannot bind my Grids for Distance + Rates and Banned Areas directly to database.
What I am doing is creating two DataTables and managing them in Viewstate through properties as follow:

        private DataTable TableDistancesAndRates {
            get {
                object objTableDistancesAndRates = ViewState["vTableDistancesAndRates"];

                if (objTableDistancesAndRates!=null) {
                    return (DataTable)objTableD

Bind gridview column to nested object



not really sure how to phrase this question.

If I have a list of contacts List<Contact> and a Contact object has an Office object property, I want to display a grid with columns

Contact Id, First Name, Last Name, Office Name

Where the sources of the above columns are respectively:

Contact.ContactId, Contact.FirstName, Contact.LastName, Contact.Office.OfficeName

I'm fine with binding the first 3, but was wondering whether I can bind the OfficeName using either Bind or Eval or whether I need to hook into the events and set the value there.






Accessing the different controls inside a GridView control

how we can access a particular control which resides inside a GridView control. In this article I will show you how you can access different controls inside a GridView control. We will see how we can access a TextBox control, a DropDownList control and a ListBox control. If you are working with ASP.NET 1.X then you might want to check out my article Accessing Different Controls Inside a DataGrid.

Working with DropDownList and ListBox Controls in ASP.NET

"In my application i am using dropdownlist to display all categories from a table. When the user clicks this control I display all records in that category. Category table also contains column called color.

I wanted display some categories in dropdown list different color and others in different color using ASP.NET."-Anonymous

How to bind TemplateField DropDownList with ArrayList?


Hi Expert!

  I'm trying gridview templateField DropDownList bind with arraylist. When i set bindsource i got this error. "//Error Object reference not set to an instance of an object."

My Sample .aspx

<asp:TemplateField HeaderText="Type">
                        <asp:DropDownList ID="_grvcboType" runat="server" Width="117px" DataTextField="Display" DataValueField="Value">
                        <asp:Label ID="Label7" runat="ser

how to bind Dropdownlist based on textbox values



I have one drodownlist which show data based om textbox values...

Let us consider i have 

Select Name from table1 where Type IN(@typ)

here @typ is textbox values

here for example textbox will have some thing like this L1,L2,L3..

How to filter Name based and shown in drodownlist based on this L1,L2,L3(on textbox)..

Help me in this

Registering controls of current assembly object reference NOT set error


Hi There,

I have created a simple ASP.net Web Application (2.0) and have added a web user control.

Now in my page I have added this control but I have registered like this,

<%@ Register Assembly="WebApplication1" Namespace="WebApplication1" TagPrefix="Custom" %>

Which means I want to access all the controls in my current Web Application project

Then I have added my user control like this,

<Custom:MyUserControl ID="MyUserControl1" runat="server" PublicProperty="Hello" />

Every thing is fine but when I execute the project, NO control on my user control gets "instantiated" and thus I receive error "Object Reference Not Set"

Note that My Control has,

1) Public property PublicProperty as String

2) 1 Label control

In Load Event Handler I am setting Text property of Label Control to PublicProperty. Here object reference NOT set error occurs (which means Label control is NOT initialized).

I don't want to register my control using Src, TagPrefix etc. Any ideas?


Change data on gridview bind


Hi there,

Simple little problem. i have a gridview that shows products assigned to a customers job. They are displayed in exVAT prices. To make it easier i want to show the inc VAT prices. How can i add VAT to 1 column in this table?

Manually coding controls such as gridview, listview, repeater, etc.


using vs2010 and .net 4

In my project I have a separate DAL built from class files that I'd like to use to store my code for CRUD operations against my database.

I'm having a hard time finding examples of how to code server controls like the gridview, listview, repeater etc., for CRUD operations manually, i.e., without a datasource control of some flavor.

I don't have any problem manually coding the referenced controls to get my data, but I do have problems when it comes to coding them to perform CRUD operations unless I use a datasource control. When I use the datasource controls CRUD operations seem to automagically appear out of nowhere after they're configured.

Can someone point me to some samples somewhere? Have I missed a section at MSDN?


bind values to dropdownlist using ASP.NET MVC



I'm using asp.net MVC 2.0, my controller coding is,

foreach(DataColumn dc in dtImport.Columns)


TempData["drpsku"] = dc.ColumnName.ToString();


i need to bind this value to the drpdown list in the aspx page.

<asp:DropDownList ID="DropDownList1" runat="server"></asp

GridView editing without using data source controls?


I am binding my gridview without using a datasource control and instead doing it in the code behind. However, I would like to use the inline editing features of the gridview. Could someone please point me to an example that does gridview editing/updating 'manually' in the code behind?


How to get object from the gridview selectindex changing Event.


I have a grid view which is populating with a data source through databind. And the Datasource is list of objects.Now when the user selected a row I want to get the object of that row.

I'm trying to get the row like this.


r = gvTruckCarrier.Rows[e.NewSelectedIndex];


drv = r.DataItem as DataRowView




but the drv value is

how do i use javascript to change values of controls inside a gridview

i have 2 labels (lblRate,lblTotal) and a textbox (txtQuantity) in a itemtemplate of a gridview.I want to calculate the products of rate and quantity and show it in Total as the textbox loses focus...i cant use textchanged because i dont want the page to postback.How do i use javascript to achieve this??Please Help Urgently....<asp:TemplateField HeaderText="Rate">                <ItemTemplate>                    <asp:Label ID="lblRate" runat="server" Text='<%# Eval("Rate") %>' />                </ItemTemplate>            </asp:TemplateField> <asp:TemplateField HeaderText="Quantity">                <ItemTemplate>                    <asp:TextBox ID="txtQty" runat="server" />                </ItemTemplate>&

how to apply validation controls on Gridview which is created dynamically

 hi, i created a gridview with two columns and 8 rows dynamically. All the rows are empty and editable at runtime. I am giving column names for this grid from some other .aspx page by passing column names using querystring.Because i am using this grid for three different pairs of column names.  i would like to know how to apply Required Field Validator, Regular Expression Validator  and Range Validator on Gridview textboxes. So that by using those validators i want to validate user inputs. And i would also like to know how to add rows at runtime for this grid. I am using ASP.net 2.0 and C#.net for this.(Visual Studio 2005) The code i am using to create gridview is below. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoadData.aspx.cs" Inherits="LoadData" %>  <form id="form1" runat="server">  <div> <asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="False" ForeColor="#333333" BorderColor="GradientInactiveCaption" BorderStyle="Solid" style="z-index: 108; left: 24px; position: absolute; top: 13px">  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  <RowStyle BackColor="#EFF3FB"

Accessing a row's dropdownlist values AFTER the gridview control is in Edit mode

Hello, I have a gridview control. When it goes into Edit mode, there are 3 dropdownlists in one cell that are populated by separate stored procedures during the RowDataBound event. The 1st dropdownlist's value is used as a parameter to determine what the 2nd dropdownlist is populated with. The 2nd dropdownlist's value is used as a parameter to determine what the 3rd dropdownlist is populated with. Everything works fine when the row is first put into Edit mode. While I'm in Edit mode and I change the selection in the 1st dropdownlist, how do I access the control so I can feed this new parameter to the 2nd dropdownlist? I have the first 2 dropdownlists set for AutoPostBack so that the Page_Load event is fired when the user changes a value. I know how to get access to these controls via the FindControl method when the row is being databound but how do I find the control during the Page_Load event? Thanks for any help!!!   Penny  
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend