I am getting some data in unstructured form. So i want it to be in structured form. So i take datatable in this i have added two columns, i.e. "field" and "value". Now i added first row in data along with the data in these two columns. But the prob is its replacing the old data every time i come again to save the data in this datatable. My code is:
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Rows.Add(val.Substring(0, val.IndexOf(":") + 1), val.Substring(val.IndexOf(":") + 1, val.Length - val.IndexOf(":") - 1))
Using DataTable, you have to create a new row before adding it:
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Value", typeof(string));
var row = dt.NewRow();
dt["Field"] = "FieldName";
dt["Value"] = "Value";
dt.Rows.Add(row);
NewRow() documentation :
You must use the NewRow method to create new DataRow objects with the
same schema as the DataTable. After creating a DataRow, you can add it
to the DataRowCollection, through the DataTable object's Rows
property. When you use NewRow to create new rows, the rows must be
added to or deleted from the data table before you call Clear.
But if you are only handling a key/value pair, why don't you use a Dictionary<string, string> ?
Are u using this in a loop? only the last line should be in the loop otherwise youre instantiating the datatable each loop.
Related
I ask myself how I can display a single row from a datatable object in a control like a gridview.
I already did it with label objects like here: (this is in load event. I already have buttons which increment the zero and decrement)
Tbname.Text = (dset.Tables("coduta").Rows(0).Item("Firma"))
TbStraße.Text = (dset.Tables("coduta").Rows(0).Item("Straße_Firma"))
TbHausnummer.Text = (dset.Tables("coduta").Rows(0).Item("Hausnummer_Firma"))
TbOrt.Text = (dset.Tables("coduta").Rows(0).Item("Ort_Firma"))
the point is I want to show the specific row in something like a gridview control. The only Idea i have is, to create a new table out of the row and that looks like a too complicated way for this. I hope guys can help
cheers steven
I am from C# background but this approach should help you.
Get first row from existing table.
Make clone of existing table.
Add that row to clone table.
Assign that table as datasource for grid
DataRow dr = dset.Tables("coduta").Rows(0);
DataTable dtNew = dset.Tables("coduta").Clone();
dtNew.Rows.Add(dr.ItemArray);
grid.DataSource = dtNew;
grid.DataBind();
try
da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.SelectCommand = new SqlCommand(#"SELECT * FROM coduta", connString);
da.Fill(ds, "coduta");
dt = ds.Tables["coduta"];
foreach (DataRow dr in dt.Rows)
{
//here is your row of data
}
I've created a DataTable _dt and added columns like below
_dt.Columns.Add("INDATE", typeof(string));
_dt.Columns.Add("INTIME", typeof(string));
_dt.Columns.Add("OUTTIME", typeof(string));
_dt.Columns.Add("OUTDATE", typeof(string));
and I stored _dt into Viewstate:
ViewState["_table"] = _dt;
and then I added a row into that DataTable like this:
DataRow _dr = _dt.NewRow();
_dr[0] = string.Format("{0:dd/MM/yyyy}", System.DateTime.Now);
_dr[3] = string.Format("{0:dd/MM/yyyy}", System.DateTime.Now);
_dt.Rows.Add(_dr);
and bound the DataTable to a gridview successfully.
My problem is: whenever I retrieve the DataTable from Viewstate
DataTable _dtTemp = (DataTable)ViewState["_table"];
the table contains DataRow also...
I don't want the dataRow.. I want table with column field only.
The DataTable object instance stored in the ViewState is the same object instance that you use when adding rows. So, whatever you do with it will be seen when you retrieve it the next time
You could use the Clone method on your original datatable stored in the ViewState and add rows to this clone. In this way you will have two distinct objects and adding rows to the second doesn't affect the first
ViewState["_table"] = _dt;
DataTable temp = _dt.Clone();
DataRow _dr = temp.NewRow();
_dr[0] = string.Format("{0:dd/MM/yyyy}", System.DateTime.Now);
_dr[3] = string.Format("{0:dd/MM/yyyy}", System.DateTime.Now);
temp.Rows.Add(_dr);
....
DataTable temp = (ViewState["_table"] as DataTable).Clone();
....
So I'm using a SQL data source that fires a Store procedure using the selected value of a drop down list as a parameter to return one of a multitude of differing tables. Then I have a gridview that uses this SQLdatasource as it's datasource to display the returned values.
What I want is to be able to get at the column names at run time so that I can use it in a later method. However the SQLDatasource doesn't seem to have a property to get at this data and when I try to get at the columns of the Datagridview after I databind it the gridview only shows a single column (the auto-generated select column). Is there a way to get at this?
I don't know about using the SQLDatasource, but you can do this easily in the codebehind if you load your data into a DataTable. E.g.
var dt = new DataTable();
var connectionString = "your connection string";
using (var db = new SqlConnection(connectionString))
{
var command = new SqlCommand("stored_procedure_name", db);
command.CommandType = CommandType.StoredProcedure;
db.Open();
var reader = command.ExecuteReader();
dt.Load(reader);
}
var columns = dt.Columns;
Each column has a ColumnName property. You can use Linq to get the column names as an array:
var columnNameArray = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();
I have a dataset that is returned from a web service. The dataset has multiple columns. Is there an easy way to assign a given column to the combobox without having to iterate through the dataset?
Something on the end of this. I have tried all sorts of properties...
ComboBox1.DataSource = suppDataSet.Tables(0)
ComboBox1.DataSource = suppDataSet.Tables(0).Columns(2).tostring
ComboBox1.DataSource = suppDataSet.Tables(0).Columns(2)
thanks for any help.
Use LINQ as follows:
ComboBox1.DataSource = From dr As DataRow In suppDataSet.Tables(0).Rows
Select CStr(dr(column_index or column_name))
Dim oList As New ArrayList
For Each oRow As DataRow In suppDataSet.Tables(0).Rows
oList.Add(oRow.Item(0))
Next
ComboBox1.DataSource = oList
Initially copy required column data in one ArrayList and assign ArrayList to ComboBox1.DataSource.
I have a GridView in ASP which I use for displaying products in a shopping cart.
I set the source of GridView the ShopDataSet and the columns Name, Price and Quantity are filled automatically.
What I want to achieve is a new column, added by me, which displays the cost of each row i.e. Cost = price * quantity;
How can I do this programatically, without executing a new query on the database?
I have to say that on Price and Quantity I set format like "${0}" for Price and "{0} piece(s)" for Quantity.
You should populate your datasource programmatically.
Get the results in a SqlDataReader and populate a DataTable with the results of the SqlDataReader.
Now create a new DataTable with columns that match the output that you want including Cost in your case. Now iterate through each row of the 1st DataTable and add rows to the new DataTable with the cost calculation.
For Example,
DataTable oldTable = new DataTable();
oldTable .Load(rdr); // where rdr is your SqlDataReader
Now create your new table
DataTable newTable = new DataTable();
DataColumn noteID = new DataColumn("Cost", typeof(string));
newTable.Columns.Add(noteID);
//Add other columns
foreach (DataRow row in oldTable .Rows)
{
DataRow newRow = newTable.NewRow();
newRow["Cost"] = //your calculation
...
...
newTable.Add(newRow);
}
Now set the DataSource of your grid to NewTable