Please bear with me, I'm new to this, so I'm sure there may be a better way to do this. But with my limited knowledge, its the only way I can think of doing it.
I have a hidden datagrid which selects the data out of a sqlserver database. It will have multiple rows (up to 10). I need to be able to read the data in and populate it into text boxes on the web page for the user to be able to modify (but not write it back to the database, it will be written to a text file to be used later). I am able to populate the text boxes easily when there is only one row of data, but with multiple rows, I can't figure out how to populate each of the text boxes.
For example for the datagrids that only return one row of data:
For Each row2 In HiddendgCompany.Rows
Dim varVrId = CType(row2.Cells(3).Text, String)
Dim varCoId = CType(row2.Cells(4).Text, String)
VrId.Text = varVrId
CoId.Text = varCoId
Next
But, how do I tell it that row zero, cell zero is equal to varInvNum0, and row one, cell zero is equal to varInvNum1, etc. I was thinking of populating an arraylist from the datagrid so I could specify which cells match up with each variable, but I have been unable to find any code that does this.
Is there a better (another) way to do this Or can it even be done Is there a way to select this data out of the database without using a datagrid I'm using Visual Developer Express' sqldatasource controls to do the selects, but I was using the datagrids to get the data, because I couldn't find any other way of selecting and setting the variables in the scripting section without the datagrid.
Thank you in advance for any and all help!
Laura

datagrid / variable problem
Daniel Walzenbach
I'd use the SqlConnection, SqlCommand and DataAdapter to fill a DataSet/DataTable. Then iterate thru every row in the datatable. For each row create a row of textboxes. Sort of like this:
int i = 0;
foreach(DataRow drDataRow in myDataTable)
{
int j = 0;
foreach(DataCell dcDataCell in drDataRow.Cells)
{
//Create textbox
TextBox oTB = new TextBox();
//Set textbox properties
oTB.Width = 100;
oTB.Name = "txt" + drDataRow.Cells[j].DataColumn + i.ToString();
oTB.Text = drDataRow.Cells[j].ToString();
//Add textbox to the for
this.Controls.Add(oTB);
//Or better yet add textbox to a flowlayout panel
this.FlowLayoutPanel1.Controls.Add(oTB);
j++;
}
i++;
}
There are lots of examples on how to use the ADO objects and how to dynamically create objects (such as textboxes) on the web and at msdn.
Harald Balik
Laura,
I see this as being solved by data binging, CurrencyManager and a Typed DataSet which contains multiple DataTables and has DataRelation(s)
So to start of, use the DataSet design and make your tables and relations, easiest way is to drag tables from the connection / server view.
Of course, you will need to have your TableStyles / ColumnStyles set up as normal.
For the first data grid (your main table)
DataBind it:
DataGrid1.DataSource = DataSet
DataGrid1.DataMember = "PrimaryTableName"
For the related ancillary table
DataGrid2.DataSource = DataSet
DataGrid2.DataMember = "PrimaryTableName.AncillaryTableRelation"
Now as your pointer changes in the first grid the second will reflect only the related data. This is true whether or not the datagrid is visible.
You can do the same with having form fields as the display of the PrimaryTable. To navigate the you access the BindingContext(DataSet, "PrimaryTableName") and use the Position member to set the record position.
I should point out that I am typing the code from memory and isn't tested completely. But the premise of this is the same, as the CurrencyManager moves position, the CurrencyManager will make all the appropriate record sources an pointers reflect that change.
CN
plavun
Do you have access to ADO.Net (objects such as DataTable, SqlConnection, SqlCommand) If so you can use them to get the data.
Are you creating the text boxes dynamically so if there is only one row of data do you only have one row of textboxes If 8 rows then 8 rows of textboxes etc...
Leon Katsnelson
Phil,
I am using a ado.net and a hidden datagrid to retreive the table data. I'm just using plain textboxes to display the data, so the user can make modifications, but I don't want it to be written back to the database. How do I tell textbox A that it is supposed to have data from column one, row one and textbox B is supposed to have data from column 2, row one, etc In the code above, I was setting variables then populating the textbox with the variable, which was easy with only one row. My issue is with the table which will bring back anywhere from 1 to 10 rows of data.
I would like to have it dynamically display the correct number of text boxes according to the number of rows retreived, but I don't know how to do this either.
Thanks,
Laura
erwind
CN,
I did try to use the BindingContext, but I got an error saying it could not be called directly, and I had to use a RaiseEvent statement to raise an event.
POLin1.DataBinding.Add(
"Text", SqlDataSource3, "POWebOutDetails.POLin")Do you know why its giving me this error Also, how do I set the postion control so it will know POLin2 should be from row 2 It doesn't look like I can do this:
POLin2.DataBinding.Add(
"Text", SqlDataSource3, "POWebOutDetails.POLin").Position = 2I am also looking into using the GroupBox command, I'll let you know if this option works.
Thanks!
Laura
carabasha
Thank you for your continued help on this.
I used your suggestion, but am trying to code it in VB (I don't know C# yet), but am getting a coding error:
Dim row3 As GridViewRow
For Each row3 In HiddengvDetails.Rows
Dim column1 As DataGridColumn
Dim int1 As Integer = 0
For Each column1 In HiddengvDetails.Columns
Dim tb1 As New TextBox
tb1.Width = 100
tb1.ID = HiddengvDetails.Columns(int1).AccessibleHeaderText
tb1.Text = HiddengvDetails.Columns(int1).ToString
Panel1.Controls.Add(tb1)
int1 = int1 + 1
Next
Next
Here is the error:
System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.BoundField' to type 'System.Web.UI.WebControls.DataGridColumn'.
It doesn't like that column1 as datagridcolumn, but I didn't know what else to set it as. Do you have any suggestions
Also, it didn't like the dynamic id name. Is there another way to do this The ID name I have listed above is not the name I want to use, but I couldn't get it to concat the name correctly (so I just put this in there figuring I'd get back to it). I tried:
tbl.ID = "txt" + HiddengvDetails.Columns(int1).AccessibleHeaderText + HiddengvDetails.Row3.
Thanks,
Laura
Grace Cheng
I've been working on the code, and I got this code to compile and run:
Dim row3 As GridViewRowFor Each row3 In HiddengvDetails.Rows
Dim column1
Dim int1 As Integer = 0
For Each column1 In HiddengvDetails.Columns
Dim tb1 As New TextBox
tb1.Width = 100
tb1.ID = "txt" & HiddengvDetails.Columns(int1).AccessibleHeaderText
tb1.Text = HiddengvDetails.Columns(int1).ToString
Panel1.Controls.Add(tb1)
int1 = int1 + 1
Next
NextBut It only returns the column headers for the 3 rows of data, instead of the data in the three rows.
Can someone please tell me what I should set tb1.Text to to get the data instead of just the headers
Thanks,
Laura