I am creating a data table and trying to bind it wit a grid view.
Code:
DataTable timeTable = new DataTable();
DataColumn colSubject = new DataColumn("Subject Name");
DataColumn colClass = new DataColumn("Class");
DataColumn colTeacher = new DataColumn("Lecturer");
DataColumn colStartTime = new DataColumn("Start");
DataColumn colEndTime = new DataColumn("End");
colSubject.DataType = System.Type.GetType("System.String");
colClass.DataType = System.Type.GetType("System.String");
colTeacher.DataType = System.Type.GetType("System.String");
colStartTime.DataType = System.Type.GetType("System.String");
colEndTime.DataType = System.Type.GetType("System.String");
timeTable.Columns.Add(colSubject);
timeTable.Columns.Add(colClass);
timeTable.Columns.Add(colTeacher);
timeTable.Columns.Add(colStartTime);
timeTable.Columns.Add(colEndTime);
int numOfRows = 0;
SqlDataReader read = null;
try
{
conn.Open();
read = getTimetable.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
if (numOfRows < 3)
{
DataRow row = timeTable.NewRow();
row[colSubject] = read["subject_name"].ToString();
row[colClass] = read["resource_name_number"].ToString();
row[colTeacher] = read["userDetail_name"].ToString();
row[colStartTime] = read["timeSlot_startTime"].ToString();
row[colEndTime] = read["timeSlot_endTime"].ToString();
timeTable.Rows.Add(row);
numOfRows++;
}
}
gridTimeTable.DataSource = timeTable;
gridTimeTable.DataBind();
}
}
Now The Data table is Created Fine and all cause I used a breakpoint to see whether it was being created the thing is it is not populating the grid view. any ideas?
Thanks :)
Related
How do I pass value of my calculated result in datagridview. In the following code I filled the 2 columns of grid by the values fetched from database through DataTable, then I add a new column named "Text". Now I want to pass a value of my calculated hash in this column but there may b some syntax problem or some mistake which causes an error occurred. Following is my code;
SqlDataAdapter sda = new SqlDataAdapter("Select Image,Hash from UserInput where PINCODE = '" + txt_LPin.Text + "'",conn);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
int nOr = dataGridView1.Rows.Count;
if (nOr == 5)
{
LoginCluePoint lcp = new LoginCluePoint();
lcp.dgv_LCP.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
lcp.dgv_LCP.RowTemplate.Height = 101;
lcp.dgv_LCP.DataSource = dt;
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Text";
int colIndex = lcp.dgv_LCP.Columns.Add(col);
lcp.dgv_LCP.Columns[2].Visible = false;
}
This problem is resolved by the following code
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Text";
int colIndex = lcp.dgv_LCP.Columns.Add(col);
dgv_LCP.Rows[0].Cells[2].Value = hash
lcp.dgv_LCP.Columns[2].Visible = false;
I have a program by VC2008 that send dataTable I sended it using the constractor I solve it help at losing data entered in dataview C# - to datagridview in another Form it work fine but I want to send only the new row I tried to copy using ImportRrow it to another datatable but it gives Form2 Blank.
and I send the data as datatable by method as following:
public DataTable showout2()
{
DataTable dtab = new DataTable();
DataColumn dc1 = new DataColumn("رقم المتسلسل");
DataColumn dc2 = new DataColumn("رقم الحساب");
DataColumn dc3 = new DataColumn("أسم الحساب");
DataColumn dc4 = new DataColumn("المالك");
DataColumn dc5 = new DataColumn("قيمة");
DataColumn dc6 = new DataColumn("نوع العملة");
DataColumn dc7 = new DataColumn("الدائن");
DataColumn dc8= new DataColumn("المدين");
DataColumn dc9= new DataColumn("تاريخ");
DataColumn dc10 = new DataColumn("تفاصيل");
dtab.Columns.Add(dc1);
dtab.Columns.Add(dc2);
dtab.Columns.Add(dc3);
dtab.Columns.Add(dc4);
dtab.Columns.Add(dc5);
dtab.Columns.Add(dc6);
dtab.Columns.Add(dc7);
dtab.Columns.Add(dc8);
dtab.Columns.Add(dc9);
dtab.Columns.Add(dc10);
DateTime date = new DateTime();
date = DateTime.Now;
// Create an array for the values.
object[] newRow = new object[10];
Set the values of the array.
string s = numb.Text;
newRow[0] = numb.Text;
newRow[1] = Account_numb.Text;
newRow[2] = Account_nam.Text;
newRow[3] = owner.Text;
newRow[4] = curency.Text;
newRow[5] = comboBox1.Text;
newRow[6] = Depet.Text;
newRow[7] = cridet.Text;
newRow[8] = date.ToString();
newRow[9] = note.Text;
declare DataRow and adding it to DataTable
DataRow row;
dtab.BeginLoadData();
// Add the new row to the rows collection.
row = dtab.LoadDataRow(newRow, true);
return dtab;
}
I transfer the data from Form as following
cashagree fm = cashagree(shouwout2());
I copy the datarow in constractor take datatable as prameter as following:
public Cashagree(DataTable d2)
{
dt.ImportRow(d2.Rows[0]);
}
here I assign the datasource and mange the secound datagridview
private void Cashagree_Load_1(object sender, EventArgs e)
{
try
{
for (int i = 0; i != dt.Rows.Count; i++){
label1.Text= dt.Rows[i].ToString();
if (string.IsNullOrEmpty(dt.Rows[i].ToString()))
{
MessageBox.Show("This is empty");
}
}
dataGridView.DataSource = dt;
dataGridView.Columns[3].Visible = false;
dataGridView.Columns[4].Visible = false;
dataGridView.Columns[5].Visible = false;
dataGridView.Columns[6].Visible = false;
dataGridView.Columns[7].Visible = false;
}
catch (Exception w) { MessageBox.Show("Error" + w.StackTrace); }
}
thank you for helping
i want to create datatable in which i want to add column names dynamically, my column names are coming from database, which is not fixed column name every time different its depend on user selection
i using sql server and c#.
Following code add column and row dynamically in the datatable:
DataTable dt = new DataTable();
var properties = typeof(model).GetProperties();
foreach (PropertyInfo p in properties)
{
dt.Columns.Add(p.Name, p.PropertyType);
}
foreach (var data in taskData)
{
var values = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
values[i] = properties[i].GetValue(data, null);
}
dt.Rows.Add(values);
}
Here is an option
var dt = new DataTable();
dt.Columns.Add("Name",typeof(string));
you may also try this one.
var dt = new DataTable();
DataColumn column = new DataColumn();
{
column.Caption = "Name";
column.ColumnName = "ColumnName";
column.DataType = typeof(String);
dt.Columns.Add(column);
}
How can I create a DataTable programatically? i have the following code and I was wanting to create a data table just like in Java that would allow me to create a table that I could file with data from a CSV (,) such as names, address, and phone numbers.
Code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private System.Data.DataSet dataSet;
System.Data.DataTable table = new DataTable("ParentTable");
public Form1()
{
InitializeComponent();
}
private void MakeParentTable()
{
DataColumn column;
DataRow row;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = true;
column.Unique = true;
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ParentItem";
column.AutoIncrement = false;
column.Caption = "ParentItem";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;
dataSet = new DataSet();
dataSet.Tables.Add(table);
for (int i = 0; i<= 2; i++)
{
row = table.NewRow();
row["id"] = i;
row["ParentItem"] = "ParentItem " + i;
table.Rows.Add(row);
}
}
private void Form1_Load(object sender, EventArgs e)
{
MakeParentTable();
}
}
}
Use FileStream and StreamReader for reading data from csv file. I see that you create DataTable programmaticaly in your code.
I cannot Display the value of "Price" cell on my gridview, here's my code:
DataColumn idCol2 = new DataColumn();
idCol2.DataType = System.Type.GetType("System.Int32");
idCol2.ColumnName = "Id";
table.Columns.Add(idCol2);
DataColumn SKUCol2 = new DataColumn();
SKUCol2.DataType = System.Type.GetType("System.String");
SKUCol2.ColumnName = "SKU";
table.Columns.Add(SKUCol2);
DataColumn ProdNameCol2 = new DataColumn();
ProdNameCol2.DataType = System.Type.GetType("System.String");
ProdNameCol2.ColumnName = "Product Name";
table.Columns.Add(ProdNameCol2);
DataColumn DescCol2 = new DataColumn();
DescCol2.DataType = System.Type.GetType("System.String");
DescCol2.ColumnName = "Product Description";
table.Columns.Add(DescCol2);
DataColumn PriceCol2 = new DataColumn();
PriceCol2.DataType = System.Type.GetType("System.Decimal");
PriceCol2.ColumnName = "Price";
table.Columns.Add(PriceCol2);
DataColumn[] keys2 = new DataColumn[5];
keys2[0] = idCol2;
table.PrimaryKey = keys2;
LivePOS livepos = new LivePOS(clientId, clientSecret);
_products = livepos.GetProducts(apiKey, token);
foreach (var p in _products)
{
table.Rows.Add(p.Id, p.Sku, p.Name, p.Description , p.SellingPrice);
}
table.DefaultView.Sort = "Id ASC";
dataGridView1.DataSource = table;
this.dataGridView1.DefaultCellStyle.Format = "N4";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[0].ReadOnly = false; //id
row.Cells[1].ReadOnly = true; //sku
row.Cells[2].ReadOnly = true; //name
row.Cells[3].ReadOnly = true; //description
}
Please help me :)Is there something wrong in this code? I created new instance of the datatable named "table" on my Form_Load event. All cell values are displaying correctly aside from this "Price" column.
Your Price Column should have Datatype of Double than Decimal.Take a look at this link
Decimal Vs Double
hope that helps