Solved

How to populate a datagrid with textboxes

Posted on 2003-11-28
5
597 Views
Last Modified: 2008-02-01
It it possible to display column information in a datagrid using just textboxes instead of text?  If so, I would like an example on how this is done.
0
Comment
Question by:Thrakrath
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:NaliniP
ID: 9841484
Thrakrath , here is the example.

First you need to bind the datasource to the datagrid. Then you have to change the code so that it will show the text boxes instead of text. look at this..

Imports System.Data.Oledb

Dim Cn as new oledbconnection
dim cmd as new oledbcommand
dim adp as new oledbdataadaptor
dim dset as new dataset
Dim Ctr as integer
dim col1 as string

cn.connectionstring="PROVIDER=MSDAORA.1;USER ID=SCOTT;PASSWORD=TIGER"
cmd.connection=cn
cmd.commandtext="select * from emp"
cmd.commandtype=Text
adp.selectcommand=cmd
adp.fill(dset)

datagrid1.datasource=dset
datagrid1.databind()

col1 = dset.tables(0).columns(0).caption 'Field Name of the First Column

for ctr=0 to datagrid1.items.count-1
   datagrid1.items(ctr).item(0).controls.add(new Literalcontrol("<INPUT TYPE=TEXT NAME=" & col1 & ctr & " VALUE=" & datagrid1.items(ctr).item(0).text & ">"))
......
......
......you can repeat for all the cell in the grid.
next

***************************
by doing the above way, you will get what you wanted. Hope this may help you.........cheers

Nalini
0
 

Author Comment

by:Thrakrath
ID: 9841756
Thanks NaliniP for your suggestion. Unfortunally I use C# so do you think you have the above example written in C# code.

If not, mabye you could help me out with the following lines of code.

These lines returns the following error during compile time:

#1
col1 = dset.Tables(0).columns(0).caption;
//'System.Data.DataSet.Tables' denotes a 'property' where a 'method' was expected

#2
for (int i = 0; i < RoutePortCalls.Items.count-1; i++)
//'System.Web.UI.WebControls.DataGridItemCollection' does not contain a definition for 'count'

#3
RoutePortCalls.items(i).item(0).controls.add(new Literalcontrol("<INPUT TYPE=TEXT NAME=" & col1 & i & " VALUE=" & datagrid1.items(i).item(0).text & ">"));
//The type or namespace name 'Literalcontrol' could not be found (are you missing a using directive or an assembly reference?)
0
 
LVL 2

Accepted Solution

by:
NaliniP earned 125 total points
ID: 9841770
you need to declare the datagrid first. I mean you have to place a datagrid control on your web form. then the above code works. there is nothing difference in the code whether it is vb.net or c#.net. only the thing is ';' and case sensitivity. so look at that..if problem come again..

Nalini
0
 

Author Comment

by:Thrakrath
ID: 9841805
I already have a datagrid that is bound to a datasource. I made a few changes to your example and now I only get one error.

In line:

RoutePortCalls.Items[i].Item[0].controls.add(new LiteralControl("<INPUT TYPE=TEXT NAME=" + col1 + i + " VALUE=" + RoutePortCalls.Items[i].Item[0].text + ">"));

I get the following error:

'System.Web.UI.WebControls.DataGridItem' does not contain a definition for 'Item'
0
 

Author Comment

by:Thrakrath
ID: 9841827
One other thing worth mentioning. I dont use a dataset when binding data to my datagrid. I'm using a method.

The code is:

private void Page_Load(object sender, System.EventArgs e)
{
     RoutePortCalls.DataSource = CreateDataSourceRoutePortCalls();
     RoutePortCalls.DataBind();
}

private ICollection CreateDataSourceRoutePortCalls()
{
     DataTable dt = new DataTable();
     DataView dv = new DataView(dt);
     DataRow dr;

     try
     {
          dt.Columns.Add(new DataColumn("Id"));
          dt.Columns.Add(new DataColumn("PortNameDataField"));
          dt.Columns.Add(new DataColumn("TerminalDataField"));
          dt.Columns.Add(new DataColumn("EndDataField"));
          dt.Columns.Add(new DataColumn("StartDataField"));

          dr = dt.NewRow();
          dr["PortNameDataField"] = "1";
          dr["PortNameDataField"] = "PortName One";
          dr["TerminalDataField"] = "Terminal One";
          dr["EndDataField"] = "2003-11-23";
          dr["StartDataField"] = "2003-12-10";
          dt.Rows.Add(dr);

          dr = dt.NewRow();
          dr["PortNameDataField"] = "2";
          dr["PortNameDataField"] = "PortName Two";
          dr["TerminalDataField"] = "Terminal Two";
          dr["EndDataField"] = "2003-12-01";
          dr["StartDataField"] = "2003-12-19";
          dt.Rows.Add(dr);
     }
     catch (Exception ex)
     {
          Response.Write (ex);
     }

     return dv;
}
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now