?
Solved

How to populate a datagrid with textboxes

Posted on 2003-11-28
5
Medium Priority
?
616 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
[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
  • 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 375 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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 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