I have a Data Table I'm using as the data source for a repeater and would like to have the results show in a random order each time it's called.
I've been able to do this while retrieving the data but wish to cache the result set before it's bound.
Is the any was to shuffle or randomise the rows of a data table before binding to the repeater?
CODE:
TreeProvider tp = new TreeProvider();
DataSet ds = new DataSet();
string sKey = "KEY";
using (CachedSection<DataSet> cs = new CachedSection<DataSet>(ref ds, 5, true, null, sKey))
{
if (cs.LoadData)
{
ds = tp.SelectNodes("", "URL", "", true, "DOCTYPE", "", "NewID()", -1, true, 5);
cs.Data = ds;
}
}
if (!DataHelper.DataSourceIsEmpty(ds))
{
rprItems.DataSource = ds.Tables[0].DefaultView;
rprItems.DataBind();
}
Any guidance is appreciated.
I ended up taking a copy of the table, adding a field and assigning a random number to each row then ordering by that row.
DataTable dt = ds.Tables[0].Copy();
if (!dt.Columns.Contains("SortBy"))
dt.Columns.Add("SortBy", typeof (Int32));
foreach (DataColumn col in dt.Columns)
col.ReadOnly = false;
Random rnd = new Random();
foreach (DataRow row in dt.Rows)
{
row["SortBy"] = rnd.Next(1, 100);
}
DataView dv = dt.DefaultView;
dv.Sort = "SortBy";
DataTable sortedDT = dv.ToTable();
rprItems.DataSource = sortedDT;
rprItems.DataBind();
You could try something like this, I know it's not pretty but:
DataTable newTable = new DataTable();
newTable.TableName = "<NewTableName>";
//Make a new Random generator
Random rnd = new Random();
while (<new table length> != <old table length>)
{
//We'll use this to make sure we don't have a duplicate row
bool rowFound = false;
//index generation
int index = rnd.Next(0, <max number of rows in old data>);
//use the index on the old table to get the random data, then put it into the new table.
foreach (DataRow row in newTable.Rows)
{
if (oldTable.Rows[index] == row)
{
//Oops, there's duplicate data already in the new table. We don't want this.
rowFound = true;
break;
}
}
if (!rowFound)
{
//add the row to newTable
newTable.Rows.Add(oldTable.Rows[index];
}
}
You'll have to use your own tables, names, and lengths of course, but this should be ok to use. If there's a lot of data, this could take a while. It's the best I can come up with, and it's untested. I'm curious to know if it works.
You could try:
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Sort");
dt.Rows.Add("TEST");
dt.Rows.Add("TEST1");
dt.Rows.Add("TEST2");
var rnd = new Random(DateTime.Now.Millisecond);
foreach (DataRow row in dt.Rows)
{
row["Sort"] = rnd.Next(dt.Rows.Count);
}
var dv = new DataView(dt);
dv.Sort = "Sort";
foreach (DataRowView row in dv)
{
Console.WriteLine(row[0]);
}
If your datatable is not too big it should do it.
Related
I need some assistance in the best way to edit my DataTable to handle possible null values.
In this instance their is potential for any of the items from the list being added to the DataTable to be null. I'm looking for the best way to account for those null values when creating the DataTable so when I insert this into MSSQL I don't run into errors.
Here is the code I'm using to generate the DataTable:
public DataTable ConvertListToCustomDataTable(List<RootObject> listOfItems)
{
DataTable table = new DataTable();
table.Columns.Add("DateCreated");
table.Columns.Add("DepthCode");
table.Columns.Add("DepthDateCreated");
table.Columns.Add("DepthLevel");
table.Columns.Add("DepthID");
table.Columns.Add("DepthCategoryName");
table.Columns.Add("DepthName");
table.Columns.Add("DepthCategoryDateUpdated");
table.Columns.Add("DepthDateUpdated");
table.Columns.Add("Name");
table.Columns.Add("ID");
table.Columns.Add("CategoryCode");
table.Columns.Add("CategoryDateCreated");
table.Columns.Add("CategoryDateUpdated");
table.Columns.Add("CategoryID");
table.Columns.Add("CategoryName");
table.Columns.Add("Code");
foreach (var item in listOfItems)
{
var row = table.NewRow();
row["DateCreated"] = item.DateCreated;
row["DepthCode"] = item.Depth.Code;
row["DepthDateCreated"] = item.Depth.DateCreated;
row["DepthLevel"] = item.Depth.Level;
row["DepthID"] = item.Depth.ID;
row["DepthCategoryName"] = item.Depth.Category.Name;
row["DepthName"] = item.Depth.Name;
row["DepthCategoryDateUpdated"] = item.Depth.Category.DateUpdated;
row["DepthDateUpdated"] = item.Depth.DateUpdated;
row["Name"] = item.Name;
row["ID"] = item.ID;
row["CategoryCode"] = item.Category.Code;
row["CategoryDateCreated"] = item.Category.DateCreated;
row["CategoryDateUpdated"] = item.Category.DateUpdated;
row["CategoryID"] = item.Category.ID;
row["CategoryName"] = item.Category.Name;
row["Code"] = item.Code;
table.Rows.Add(row);
}
return table;
}
You need to set the table columns to accept nulls.
DataColumn datecolumn = new DataColumn("DateCreated");
datecolumn.AllowDBNull = true;
I would put the Column names in an array and loop through them, ie something similar to this.
DataTable table = new DataTable();
string[] column = { "DateCreated", "DepthCode", "DepthDateCreated" };
foreach (var item in column)
{
DataColumn datecolumn = new DataColumn(item);
datecolumn.AllowDBNull = true;
table.Columns.Add(item);
}
I have correct source DataTable "sourceDataTable" and I call method to split it into several and store the result into DataSet "ds":
DataSet ds = MyClass.SplitDataTables(sourceDataTable);
Here is the method MyClass.SplitDataTables():
public static DataSet SplitDataTables(DataTable sourceDataTable)
{
using (DataSet dsOut = new DataSet())
{
DataTable dt1 = new DataTable;
DataTable dt2 = new DataTable;
DataTable dt3 = new DataTable;
dt1 = sourceDataTable.Clone();
dt2 = sourceDataTable.Clone();
dt3 = sourceDataTable.Clone();
foreach (DataRow row in sourceDataTable.Rows)
{
//column is for example "City" and some row has "Boston" in it, so I put this row into dt1
if (row["ColumnName"].ToString() == "something")
{
dt1.ImportRow(row);
}
else if (...)
{ } //for other DataTables dt2, dt3, etc...
else .......... ;
}
//here I put resulting DataTables into one DataSet which is returned
string[] cols= { "dt1", "dt2", "dt3" };
foreach (string col in cols)
{
dsOut.Tables.Add(col);
}
return dsOut;
}
}
So with this returned DataSet I display new Windows each with one DataTable
foreach (DataTable dtt in ds.Tables)
{
string msg = dtt.TableName;
Window2 win2 = new Window2(dtt, msg);
win2.Show();
}
All I get shown is Windows with placeholder for "empty DataGrid"
Windows code is correct, as it works whith "unsplit DataTable".
I assume code in splitting DataTables is all wrong as it does not output DataSet with filled DataTables. I will greatly appreciate any help on this issue. Thank you!
You don't need a for loop here
Replace below code
string[] cols= { "dt1", "dt2", "dt3" };
foreach (string col in cols)
{
dsOut.Tables.Add(col);
}
With this
dsOut.Tables.Add(dt1);
dsOut.Tables.Add(dt2);
dsOut.Tables.Add(dt3);
Thanks to #Krishna I got this solved. So if you ever encounter similar problem here are 2 things to note:
string[] cols = { "dt1", "dt2", "dt3", ... };
foreach (string col in cols)
{
dsOut.Tables.Add(col);
}
This cycle does not have access to objects of DataTables with same name and writes only empty DataTables into DataSet collection (regardless of same name!).
If you create new DataTable and you will be making it a clone of another DataTable, dont bother yet with setting its name.
DataTable dt1 = new DataTable();
Make new DataTable of same format as source DataTable:
dt1 = sourceDataTable.Clone();
dt2 = sourceDataTable.Clone();
//etc...
Now you have to set unique DataTable names to each DataTable cloned from source DataTable:
dt1.TableName = "Name1";
dt2.TableName = "Name2";
//and so on
Now all works as intended.
I am trying to explain what I need to do.
As you can see in the second foreach, I am iterating over the temporary data table, but I need to set a value for the same row in the original data tablerow.
For example:
_uc090_WingsIntegrationDataSet.WingsBookingInterface[0]["property"] = x;
What I dont know how to implement is how to find that row and set the property, I saw the LoadRow method but I never used it before.
DataTable tempTable = _uc090_WingsIntegrationDataSet.WingsBookingInterface.Clone();
DataRow[] datarows = _uc090_WingsIntegrationDataSet.WingsBookingInterface.Select("REFMDossierID = " + refmDossierId);
if (datarows.Length > 0)
{
foreach (DataRow dr in datarows)
{
tempTable.ImportRow(dr);
}
}
//2. foreach master row
foreach (UC090_WingsIntegrationDataSet.WingsBookingInterfaceRow row in tempTable.Rows)
You can find the row using Rows.Find(), but it requires that a PrimaryKey be set on at least one column in your DataTable.
As far as loading new data, you can use LoadDataRow() which will update existing rows (if a primary key is supplied) or insert new data if any matching datatypes are found.
Please take a look at the following example using untyped datasets:
DataSet dataSet = new DataSet("MyDataSet");
DataTable dataTable = dataSet.Tables.Add("JavaScriptLibraries");
DataColumn[] dataColumns =
new[] {
new DataColumn("Id", typeof(Int32))
{
AutoIncrement = true,
AllowDBNull = false,
AutoIncrementSeed = 1
},
new DataColumn("Name", typeof(String))
};
dataTable.Columns.AddRange(dataColumns);
dataTable.PrimaryKey = new[] { dataTable.Columns["Id"] };
DataRow dataRow1 = dataTable.NewRow();
dataRow1["Name"] = "jQuery";
dataTable.Rows.Add(dataRow1);
DataRow dataRow2 = dataTable.NewRow();
dataRow2["Name"] = "MooTools";
dataTable.Rows.Add(dataRow2);
// Copy the dataset
DataSet tempDataSet = dataSet.Clone();
DataTable tempDataTable = tempDataSet.Tables["JavaScriptLibraries"];
DataRow[] tempRows = dataSet.Tables["JavaScriptLibraries"].Select("Name = 'jQuery'");
// Import rows to copy of table
foreach (var tempRow in tempRows)
{
tempDataTable.ImportRow(tempRow);
}
foreach (DataRow tempRow in tempDataTable.Rows)
{
// Find existing row by PK, then update it
DataRow originalRow = dataTable.Rows.Find(tempRow["Id"]);
originalRow["Name"] = "Updated Name";
}
// Load new data using LoadDataRow()
object[] newRow = new[] { null, "New Row" };
dataTable.BeginLoadData();
dataTable.LoadDataRow(newRow, true);
dataTable.EndLoadData();
I have a DataView which has two columns: ContactID, and Name
How can I check if a particular ContactID is already existing in the DataView?
Have you had a look at DataView.FindRows Method or maybe DataView.Find Method
The DataView has a method called FindRows, this can be used to search for a specific contactID, for example...
var table = new DataTable();
var column = new DataColumn("Id", typeof (int));
table.Columns.Add(column);
table.PrimaryKey = new[] {column}; // Unique Constraint
var row = table.NewRow();
row["Id"] = 100;
table.Rows.Add(row);
row = table.NewRow();
row["Id"] = 200;
table.Rows.Add(row);
var view = new DataView(table) { ApplyDefaultSort = true };
var rows = view.FindRows(200);
foreach(var r in rows)
{
Console.WriteLine(r["Id"]);
}
Use the Following code to Find the row in the Dataview
//Your original Table Which consist of Data
DataTable dtProducts = new DataTable();
//Add the DataTable to DataView
DataView ProductDataView = new DataView(dtProducts);
ProductDataView.RowFilter = "";
ProductDataView.Sort = "ProdId";
int recordIndex = -1;
//In the Find Row Method pass the Column
//value which you want to find
recordIndex = ProductDataView.Find(1);
if (recordIndex > -1)
{
Console.WriteLine("Row Found");
}
How to 'union' 2 or more DataTables in C#?
Both table has same structure.
Is there any build-in function or should we do manually?
You are looking most likely for the DataTable.Merge method.
Example:
private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable("Items");
// Add columns
DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));
table1.Columns.Add(idColumn);
table1.Columns.Add(itemColumn);
// Set the primary key column.
table1.PrimaryKey = new DataColumn[] { idColumn };
// Add RowChanged event handler for the table.
table1.RowChanged += new
System.Data.DataRowChangeEventHandler(Row_Changed);
// Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = table1.NewRow();
row["id"] = i;
row["item"] = i;
table1.Rows.Add(row);
}
// Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values");
// Create a second DataTable identical to the first.
DataTable table2 = table1.Clone();
// Add column to the second column, so that the
// schemas no longer match.
table2.Columns.Add("newColumn", typeof(System.String));
// Add three rows. Note that the id column can't be the
// same as existing rows in the original table.
row = table2.NewRow();
row["id"] = 14;
row["item"] = 774;
row["newColumn"] = "new column 1";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
row["newColumn"] = "new column 2";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
row["newColumn"] = "new column 3";
table2.Rows.Add(row);
// Merge table2 into the table1.
Console.WriteLine("Merging");
table1.Merge(table2, false, MissingSchemaAction.Add);
PrintValues(table1, "Merged With table1, schema added");
}
private static void Row_Changed(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine("Row changed {0}\t{1}", e.Action,
e.Row.ItemArray[0]);
}
private static void PrintValues(DataTable table, string label)
{
// Display the values in the supplied DataTable:
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.Write("\t " + row[col].ToString());
}
Console.WriteLine();
}
}
You could try this:
public static DataTable Union (DataTable First, DataTable Second)
{
//Result table
DataTable table = new DataTable("Union");
//Build new columns
DataColumn[] newcolumns = new DataColumn[First.Columns.Count];
for(int i=0; i < First.Columns.Count; i++)
{
newcolumns[i] = new DataColumn(
First.Columns[i].ColumnName, First.Columns[i].DataType);
}
table.Columns.AddRange(newcolumns);
table.BeginLoadData();
foreach(DataRow row in First.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
foreach(DataRow row in Second.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
table.EndLoadData();
return table;
}
From here (not tested).
You could use Concat from Linq to datasets (get the free chapter of LINQ in Action) to join them and then .AsDataTable to create the table (assuming you actually want them as a DataTable)
Stumbled across this question, and Ruben Bartelink gave a great answer, but with no code. So I had to look it up elsewhere, which defeats the point of StackOverflow. Now that it's 2010, the other answers given aren't quite as viable. For reference, here's code demonstrating the CopyToDataTable() extension method. It's in VB so as to not steal the credit from Ruben if he wants to revisit the past and post a more complete answer :)
Public Function GetSchema(ByVal dbNames As IEnumerable(Of String)) As DataTable
Dim schemaTables As New List(Of DataTable)()
For Each dbName As String In dbNames
Dim cnnStr = GetConnectionString(dbName)
Dim cnn As New SqlConnection(cnnStr)
cnn.Open()
Dim dt = cnn.GetSchema("Columns")
cnn.Close()
schemaTables.Add(dt)
Next
Dim dtResult As DataTable = Nothing
For Each dt As DataTable In schemaTables
If dtResult Is Nothing Then
dtResult = dt
Else
dt.AsEnumerable().CopyToDataTable(dtResult, LoadOption.PreserveChanges)
End If
Next
Return dtResult
End Function
Try this using Linq to DataSet, must add the reference for System.Data.DataSetExtensions.dll, another approach, alternative for DataTable.Merge method).
static void Main(string[] args)
{
DoUnion();
}
private static void DoUnion()
{
DataTable table1 = GetProducts();
DataTable table2 = NewProducts();
var tbUnion = table1.AsEnumerable()
.Union(table2.AsEnumerable());
DataTable unionTable = table1.Clone();
foreach (DataRow fruit in tbUnion)
{
var fruitValue = fruit.Field<string>(0);
Console.WriteLine("{0}->{1}", fruit.Table, fruitValue);
DataRow row = unionTable.NewRow();
row.SetField<string>(0, fruitValue);
unionTable.Rows.Add(row);
}
}
private static DataTable NewProducts()
{
DataTable table = new DataTable("CitricusTable");
DataColumn col = new DataColumn("product", typeof(string));
table.Columns.Add(col);
string[] citricusFruits = { "Orange", "Grapefruit", "Lemon", "Lime", "Tangerine" };
foreach (string fruit in citricusFruits)
{
DataRow row = table.NewRow();
row.SetField<string>(col, fruit);
table.Rows.Add(row);
}
return table;
}
private static DataTable GetProducts()
{
DataTable table = new DataTable("MultipleFruitsTable");
DataColumn col = new DataColumn("product", typeof(string));
table.Columns.Add(col);
string[] multipleFruits = { "Breadfruit", "Custardfruit", "Jackfruit", "Osage-orange", "Pineapple" };
foreach (string fruit in multipleFruits)
{
DataRow row = table.NewRow();
row.SetField<string>(col, fruit);
table.Rows.Add(row);
}
return table;
}
antonio