Solved

How to populate a datagrid with textboxes

Posted on 2003-11-28
5
614 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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