I have a DataTable which has several rows already. Now I want to add a column and populate values for that column alone in all the rows. One way is to iterate over all the rows and put values for that column alone. Is there any other better way of doing this?
Related
I have a datagridview that is populated by a combo box with 1 column. I need to be able to select that record and insert values not shown in the datagridview. Normally I would do this when I populate the all the values needed:
newlien.LienReason = (string)row.Cells["LienType"].Value;
newlien.LienNumber = (string)row.Cells["LienNumber"].Value;
Which will not work as the cells are not being populated. I am guessing that there are two solutions: either add the other columns and hide them and then pull the values from hidden cells or some way to get the values of the record without adding the values to the DGV. Either way I have no idea how to do it!
The easiest way to do this is to add the desired column(s) to your DGV and then set visible to false in those DGV column properties. Then you can still pull values from the hidden columns, just like you would from any other column:
//hidden column
newlien.AnotherVariable = (string)row.Cells["HiddenColumn"].Value;
Let me know if this helps.
Here's a scenario.
I have a 5 rows and 3 columns table having Textboxes in the UI. The values of each row may or may not be entered. Consider a possibility that all the values in the 5*3 tables are entered, so in code-behind i need to capture those values, bind it in a control(may be list control or an Array or Datarow) and make the loop to be iterated so as i can add values to the DB.
The loop concept is there for the sake - if a user didn't enter values in all the columns. How should i accomplish this??
P.S. - I am inserting those values in the Database afterwords.
Use DataTable
Add rows and then add them in the grid. iterate loop through Gridview rows.
I have a C# program that selects data from two different database files and combines the data that I need into a datatable (dt). All the information I need is in that datatable, and I want to put it into a datagridview. Besides the information in the datatable, I also have two columns in the datagridview that I'll calculate as I add each row to the datagridview (dataGridView1).
My question is: How can I get my datatable (dt) into the datagridview (dataGridView1)? Would I do something like this?:
dataGridView1.column("MemberSep") = dt.column("MBRNUM);
I'm thinking that I could loop through the datatable, calculate the values for my first two columns of the datagridview, and then write it all to a row until I've read the whole datatable. I've never worked with a datagridview control before. Any help would be greatly appreciated.
Set the DataGridView's DataSource to the DataTable.
You'll need to use data binding.
http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx
DataTable table = new DataTable();
//add in tables
table.Columns.Add("Column 1", typeof(int));
table.Columns.Add("Column 2", typeof(int));
//add in rows
table.Rows.Add(1, 2);
This would just mean iterating through the dbf's and grabbing the column names then inserting them with 2 for each (column) loops, then add in your custom columns you need. Also could drop any linking columns that are identical in each column.
Then 2 more for each (row) loops and you can populate the data rows.
All that is left is a for each row in table to calculate the custom column values for that row.
Depending on the relationships of the dbf's you can just tweak the loops.
If you want to manually fill datagridview I suggest to use strong typed datatables and rows from Dataset. In this case you don't have to worry about column names.
I have datagrid view in which I have 3 columns. The are name, price, and quantity. I have populated the first 2 columns from a dataset and now I want to assign a value to quantity the column based on the data in the existing columns. How can I do that?
You can use Expression based columns.
Here is a good article: Express Yourself with Expression-based Columns
You can also take a look at Datatable.Compute method.
I am retrieving a column from a table using an SqlDataSource in ASP.NET/C#, which contains multiple 'values' separated by commas. I'd like to be able to separate this values into multiple columns in my GridView, and also allow them to update them. Is this easy to accomplish?
Thanks
I would prefer to split the values to multiple columns before retrieving data from SQL.
But if you cant do that then you will have to loop through every row and separate the values in your datatable before you bind your data to the gridview.