I have one array.
I save this array to DataTable.
My code like:
string[] myResult;
DataTable dt = new DataTable();
dt.Columns.Add("myCategory");
for (int i = 0; i < myResult.Length; i++)
{
DataRow row = dt.NewRow();
row[0] = myResult[i];
dt.Rows.Add(row);
}
My data table like:
myCategory:
-+-+-+-+-+-+-+-+-+-
Student
Micheal
7.5
9.5
6.5
But I want this save like:
Category Name Score 1 Score 2 Score 3
Student Micheal 7.5 9.5 6.5
How to add columns like this.
You are always assigning the value to the first column with row[0]. Maybe you want to create a table with a single DataRow:
string[] myResult; // initialize ....
DataTable dt = new DataTable();
foreach(string s in myResult)
dt.Columns.Add(); // or a named column, but you haven't provided any informations
DataRow row = dt.Rows.Add(); // already added
for (int i = 0; i < myResult.Length; i++)
row.SetField(i, myResult[i]);
DataColumnCollection.Add() adds columns with a default name ("Column1", "Column2", ...).
Create a class to clear code
class MyResult
{
public String Category { get; set; }
public String Name { get; set; }
public float Score1 { get; set; }
public float Score2 { get; set; }
public float Score3 { get; set; }
}
Write the below code in your function.
List<MyResult> result = new List<MyResult>();
MyResult r1 = new MyResult
{
Category = "Student",
Name = "Micheal",
Score1 = 7.5f,
Score2 = 9.5f,
Score3 = 6.5f
};
result.Add(r1);
DataTable dt = new DataTable();
dt.Columns.Add("Category");
dt.Columns.Add("Name");
dt.Columns.Add("Score1");
dt.Columns.Add("Score2");
dt.Columns.Add("Score3");
foreach (MyResult item in result)
{
DataRow row = dt.NewRow();
row["Category"] = item.Category;
row["Name"] = item.Name;
row["Score1"] = item.Score1;
row["Score2"] = item.Score2;
row["Score3"] = item.Score3;
dt.Rows.Add(row);
}
You need more columns and also you should add your new row to your dt after the loop. So this should be what you want:
string[] myResult = {"Student" , "Micheal" , "7.5" , "9.5" , "6.5"};
DataTable dt = new DataTable();
dt.Columns.Add("myCategory");
dt.Columns.Add("Name");
dt.Columns.Add("Score 1");
dt.Columns.Add("Score 2");
dt.Columns.Add("Score 3");
DataRow row = dt.NewRow();
for (int i = 0; i < myResult.Length; i++)
{
row[i] = myResult[i];
}
dt.Rows.Add(row);
The result in a DataGridView:
Related
I have a table which containes many columns , in case I can have 5 row or 6 rows n rows.
In my code I want to generate a list according to table's rows
For example I have car1 table which containes these elements :
Id ColumnName
1 Ford
2 Fiesta
3 Ford
For example I have car2 table which containes these elements :
Id ColumnName
1 Mercedes
2 BMW
3 Citroen
4 Renault
5 Kia
Each time when I executed code I have to create a generic list which consist of these tables elements .
How can I achieve this ?
Thank you
Here it is:
public void StudentList()
{
// DataTable dt = new DataTable("Branches");
DataTable dt = new DataTable("Student");
dt.Columns.Add("StudentId", typeof(Int32));
dt.Columns.Add("StudentName", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.Columns.Add("MobileNo", typeof(string));
//Data
dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000");
dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
dt.Rows.Add(3, "Namit", "Pune", "1222222222");
dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
List<Student> studentList = new List<Student>();
for (int i = 0; i < dt.Rows.Count; i++)
{
Student student = new Student();
student.StudentId = Convert .ToInt32 (dt.Rows[i]["StudentId"]);
student.StudentName = dt.Rows[i]["StudentName"].ToString();
student.Address = dt.Rows[i]["Address"].ToString();
student.MobileNo = dt.Rows[i]["MobileNo"].ToString();
studentList.Add(student);
}
}
//Generate Sample DataTable
DataTable dt = new DataTable("car1");
dt.Columns.Add("Id", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
//Inserting Sample Data
dt.Rows.Add(1, "Mercedes");
dt.Rows.Add(2, "BMW");
dt.Rows.Add(3, "Citroen");
dt.Rows.Add(4, "Renault");
dt.Rows.Add(5, "Kia");
This is the modern approach for creating a List in C#.
For this example I am creating a simple car1 class like:
public class car1
{
public int Id{ get; set; }
public string Name{ get; set; }
}
List<car1> carList = new List<car1>();
carList = (from DataRow dr in dt.Rows
select new car1()
{
Id= Convert .ToInt32 (dr["Id"]),
Name= dr["Name"].ToString()
}).ToList();
Convert DataTable to List using a Generic Method
private static List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}
private static T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null);
else
continue;
}
}
return obj;
}
List<car1> car1Details = new List<car1>();
car1Details = ConvertDataTable<car1>(dt);
Also see this - https://www.c-sharpcorner.com/UploadFile/ee01e6/different-way-to-convert-datatable-to-list/
This is going to be my text file (30 lines)
OrderNo:37374
OrderQuantity:250
BarcodeQR:584,25478Klkd28
NormalBarcode:1565484864
.......
.......
.......
This is the code :
public DataTable DTforReport()
{
DataTable dt = new DataTable();
DataColumn col = new DataColumn("test");
col.DataType = System.Type.GetType("System.String");
dt.Columns.Add(col);
string[] lines = File.ReadAllLines("C:\\Users\\abc\\Desktop\\abc.txt");
foreach (var line in lines)
{
var segments = line.Split(';');
foreach (var seg in segments)
{
DataRow dr = dt.NewRow();
dr[0] = seg;
dt.Rows.Add(dr);
}
}
return dt;
}
I want my output like this
OrderNo OrderQuantity BarcodeQR
37374 250 584,25478Klkd28
How can I change my code to achieve this?
You have generated only one column. Change your code like below to see your desired result:
public DataTable DTforReport()
{
DataTable dt = new DataTable();
string[] lines = File.ReadAllLines("C:\\Users\\abc\\Desktop\\abc.txt");
DataRow dr = dt.NewRow();
for (int i = 0; i < lines.Length; i++)
{
DataColumn col = new DataColumn(lines[i].Split(':')[0]);
col.DataType = Type.GetType("System.String");
dt.Columns.Add(col);
var segment = lines[i].Split(':')[1];
dr[i] = segment;
}
dt.Rows.Add(dr);
return dt;
}
I suggest you to modify your method like the following:
public DataTable DTforReport()
{
DataTable testTable = new DataTable("Test");
testTable.Columns.Add("OrderNo");
testTable.Columns.Add("OrderQuantity");
testTable.Columns.Add("BarcodeQR");
string[] lines = File.ReadAllLines("C:\\Users\\abc\\Desktop\\abc.txt");
foreach (var line in lines)
{
DataRow dRow = testTable.NewRow();
var segments = line.Split(';');
for (int i = 0; i < segments.Length; i++)
{
var colValues = segments[i].Split(':');
dRow[i] = colValues[1];
}
testTable.Rows.Add(dRow);
}
return testTable;
}
Few suggestions for improvement:
I have given static column names, if you want to add more or they may change in future means you can create dynamic columns in the datatable.
If you have doubts in the input values, make use of proper validation
Validations in the sense, make sure about the splitted values before accessing them through index otherwise they may ends up with IndexOutOfRangeException
DataTable dt = new DataTable();
string[] lines = File.ReadAllLines("C:\\Users\\abc\\Desktop\\abc.txt");
var firstLine = lines.First();
var columns = firstLine.Split(';');
for (var icount = 0; icount < columns.Count(); icount++)
{
var colName = columns[icount].Contains(":") ? columns[icount].Split(':')[0] : "Column" + icount;
var dataCol = new DataColumn(colName);
dataCol.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dataCol);
}
foreach (var line in lines)
{
DataRow dr = dt.NewRow();
var segments = line.Split(';');
for (var icount = 0; icount < segments.Count(); icount++)
{
var colVal = segments[icount].Contains(":") ? segments[icount].Split(':')[1] : "";
dr[icount] = colVal;
}
dt.Rows.Add(dr);
}
*Number of column must be same in each row.
I want to continue to add data to datatable that already contains data.
In this method, it was get data like: dt = { 1, 2 }.
public class GetRowOne()
{
// some code
if(contains == null)
{
DataRow dr = dt.NewRow();
string[] array1 = lstHeader[1].ToArray();
for (int i = 0; i < 8; i++)
dr[i] = array1[i];
}
// some code
}
dt.Rows.Add(dr.ItemArray);
Now, in another method, I also create and run same like this code.
public class GetRowTwoAndThree()
{
// some code
DataRow dr = dt.NewRow();
string[] array1 = lstHeader[1].ToArray();
for (int i = 0; i < 8; i++)
dr[i] = array1[i];
// some code
}
dt.Rows.Add(dr.ItemArray);
It return new values in dt is: dt = { 4, 5 }
I think error at like: DataRow dr = dt.NewRow(); or line: dt.Rows.Add(dr.ItemArray);
You will look: when dt.Rows.Add(dr.ItemArray). All before data will null, it only add new values to dt.
I want dt save old data and new data, it should be:
dt = { 1, 2, 3, 4 }
Store Data in ViewState["OldData"] and after adding new row again store dt in ViewState["OldData"].
If in same page you are calling method then store that in viewstate, you can also store in session or create get set method for that datatable
DataTable dt{get; set;}
EDIT-:
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
dt.Columns.Add("Column1");
dttest = dt;
}
public void GetRowOne()
{
DataRow dr = dt.NewRow();
dr["Column1"] = "Test";
dt.Rows.Add(dr);
dttest = dt;
}
DataTable dttest { get; set; }
private void button1_Click(object sender, EventArgs e)
{
GetRowOne();
}
i have this piece of code through which i am inserting one grid view data to another
private void btnADD_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("LOB");
dt.Columns.Add("Quantity");
dt.Columns.Add("Name");
dt.Columns.Add("Packing");
dt.Columns.Add("Price");
dt.Columns.Add("Code");
dr = dt.NewRow();
dr["LOB"] = txtLOB.Text;
dr["Quantity"] = txtQuantity.Text;
dr["Name"] = txtName.Text;
dr["Packing"] = txtPacking.Text;
dr["Price"] = txtPrice.Text;
dr["Code"] = txtBachNo.Text;
dt.Rows.Add(dr);
gridviewDtaInserted.DataSource = dt;
}
i am able to insert one row at a time but i want to insert many rows one after another.
You should declare DataTable as globally because every time on Button click it has instantiated with new key word.
Try this:
DataTable dt = new DataTable();
dt.Columns.Add("LOB");
dt.Columns.Add("Quantity");
dt.Columns.Add("Name");
dt.Columns.Add("Packing");
dt.Columns.Add("Price");
dt.Columns.Add("Code");
private void btnADD_Click(object sender, EventArgs e)
{
DataRow dr;
for(int i = 0; i <= RowsCountThatYouWantToIsert; i++)
{
dr = dt.NewRow();
dr["LOB"] = txtLOB.Text;
dr["Quantity"] = txtQuantity.Text;
dr["Name"] = txtName.Text;
dr["Packing"] = txtPacking.Text;
dr["Price"] = txtPrice.Text;
dr["Code"] = txtBachNo.Text;
dt.Rows.Add(dr);
}
gridviewDtaInserted.DataSource = dt;
}
IF you are reading from one view to another you can use a looping structure to go through each iteration. I would suggest a for loop so that you can use the current numerical iteration as part of the instruction. if you want to ammend the first view to the second then you may want to use
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
dt=(DataTable)DataGridViewer1.datasource;
dt2=(DataTable)DataGridViewer2.datasource;
dt2.Merge(dt);
DataGridViewer2.datasource=dt2;
The DataSource property, from a DataGridView accepts a collection of objects.
So, I would suggest you to add how many rows you want to that public collection of objects, and at the end to update the DataSource
gridviewDtaInserted.DataSource = myCollection;
See here more info: MSDN
Also, here is a nice question may help you.
As a skeleton you can design in this way:
public class TestFunctional {
public TestFunctional(){
DataItems = new List<DataItem>();
}
public List<DataItem> DataItems { get; set; }
private void AddOneItem(){
var newItem = new DataItem {
LOB = "a",
Quantity = 1,
Name = "A",
Packing = true,
Code = "a1"
};
DataItems.Add(newItem);
RefreshGrid();
}
private void AddMultipleItems(){
var newItem1 = new DataItem {
LOB = "a",
Quantity = 1,
Name = "A",
Packing = true,
Code = "a1"
};
var newItem2 = new DataItem {
LOB = "b",
Quantity = 2,
Name = "B",
Packing = false,
Code = "b2"
};
DataItems.Add(newItem1);
DataItems.Add(newItem2);
/*or use DataItems.AddRange( ... ) */
RefreshGrid();
}
private void RefreshGrid()
{
gridviewDtaInserted.Rows.Clear();
gridviewDtaInserted.Refresh();
gridviewDtaInserted.DataSource = DataItems;
}
}
public class DataItem{
public string LOB { get; set; }
public double Quantity { get; set; }
public string Name { get; set; }
public bool Packing { get; set; }
public decimal Price { get; set; }
public string Code { get; set; }
}
I hope it will help you. Otherwise, ask :)
Edit:
Also, try to use the BindingList instead of List, I am not sure, but maybe it will automatically update the DataSource of the grid as soon an item is inserted in the collection.
BindingList<DataItem> DataItems
My Datagrid is filled with the right number of rows, but there are no data show.
All the row are display empty cols.
What could be the reason for that ?
basedon
It is the first time I use a datagridview.
public void BindDataGridView(DataGridView dgv, Hashtable ht) {
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("test");
//now build our table
dt.Columns.Add("col1", typeof(string));
dt.Columns.Add("col2", typeof(Int32));
IDictionaryEnumerator enumerator = ht.GetEnumerator();
DataRow row = null;
while (enumerator.MoveNext()) {
string index = (string)enumerator.Key; // boekingsREf
MyClass a = (MyClass)enumerator.Value;
row = dt.NewRow();
row["col1"] = index;
row["col2"] = a.number;
dt.Rows.Add(row);
}
//dgv.DataSource = ds.Tables[0];
dgv.DataSource = ds.Tables[0];
}
First example:
public Form1()
{
InitializeComponent();
Hashtable ht = new Hashtable();
ht[1] = "One";
ht[2] = "Two";
ht[3] = "Three";
BindDataGridView(dataGridView1, ht);
}
public void BindDataGridView(DataGridView dgv, Hashtable ht)
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("test");
//now build our table
dt.Columns.Add("col1", typeof(int));
dt.Columns.Add("col2", typeof(string));
foreach (DictionaryEntry dictionaryEntry in ht)
{
int index = (int)dictionaryEntry.Key;
string value = (string)dictionaryEntry.Value;
DataRow row = dt.NewRow();
row["col1"] = index;
row["col2"] = value;
dt.Rows.Add(row);
}
dgv.DataSource = ds.Tables[0];
}
Second example:
Assuming your MyClass is
public class MyClass
{
public int number { get; set; }
static public implicit operator MyClass(int value)
{
return new MyClass() { number = value };
}
}
and the hashtable is (reverse keys/values)
Hashtable ht = new Hashtable();
ht["One"] = 1;
ht["Two"] = 2;
ht["Three"] = 3;
and you change this line from your post code
MyClass a = (int)enumerator.Value;