Solved

How to populate a datagrid with textboxes

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

896 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

13 Experts available now in Live!

Get 1:1 Help Now