setOrdinal not working properly when table is bind to datasource - c#

When I try to add new column and use SetOrdinal(2)
The newly added column was positioned in the last column
conn = new MySqlConnection(connectString);
conn.Open();
fireAdapter = new MySqlDataAdapter(query, conn);
fireBuilder = new MySqlCommandBuilder(fireAdapter);
fireDataTable = new DataTable();
fireAdapter.Fill(fireDataTable);
fireSource = new BindingSource();
fireSource.DataSource = fireDataTable;
grid.DataSource = fireSource;
conn.Close();
DataColumn newcol = new DataColumn("Blah", typeof(string));
fireDataTable.Columns.Add(newcol);
newcol.SetOrdinal(2);

I'm pretty sure that your code does what you expect, it changes the position of the new column in the DataTable. But it is still shown as last column in the grid. So why you don't initialize the BindingSource after you have fully initialized the DataTable?
DataColumn newcol = new DataColumn("Blah", typeof(string));
fireDataTable.Columns.Add(newcol);
newcol.SetOrdinal(2);
// and now start assign it to the BindingSource

Related

New columns added to datatable are not showing up in datagridview

I have created a datatable then manually added two new columns in position 0 and 1, and given them default values. When I loop through the datatable it prints all the values out correctly and everything seems to be there. but when I pass the datatable to a DataGridView via a binding source it doesn't show the two new columns in the datagridview. Any idea as to what im doing wrong?
Regards
Amarino
DataTable lDT2 = Conn.ExecuteStoredProcedureValidation(lDT);
DataColumn newColumn1 = new DataColumn("TestName", typeof(string));
DataColumn newColumn2 = new DataColumn("SheetName", typeof(string));
newColumn1.DefaultValue = "test";
newColumn2.DefaultValue = "test2";
lDT2.Columns.Add(newColumn2);
lDT2.Columns.Add(newColumn1);
lDT2.Columns["TestName"].SetOrdinal(0);
lDT2.Columns["SheetName"].SetOrdinal(1);
DataGridView lDGV = new DataGridView();
lDGV.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;
lDGV.RowHeadersVisible = false;
BindingSource BindingSource1 = new BindingSource(); //create new data binding source
BindingSource1.DataSource = lDT2; //SetData source change to LDT2
lDGV.DataSource = BindingSource1;
lDGV.RowHeadersVisible = true;
lDGV.Tag = page.Controls[0].Tag;
lDGV.AccessibleName = page.Controls[0].AccessibleName;
lFormV.tabControl_Val.TabPages[page.Name].Show();
lFormV.tabControl_Val.TabPages[page.Name].Controls.Add(lDGV);

How to add row with default values into System.Data.DataTable

I have DataTable and I need to add row with default values and save it into database.
BONUS: Would be awesome to auto increment key column.
How can I achieve that?
static int i = 1;
private void CreateDefault()
{
DataColumn column = null;
DataTable table = new DataTable();
column = new DataColumn();
var Col1 = column;
Col1.DataType = System.Type.GetType("System.Int32");
Col1.DefaultValue = i;
Col1.Unique = false;
table.Columns.Add(column);
i = i + 1;
column = new DataColumn();
var Col2 = column;
Col2.DataType = System.Type.GetType("System.String");
Col2.DefaultValue = "Your String";
table.Columns.Add(column);
DataRow row = null;
row = table.NewRow();
table.Rows.Add(row);
}
Variable i will be auto increment key column. Now Insert the DataTable to DataBase.
You can add a new row with default values and save it into database like this.
using (var conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM YourTableName", conn);
SqlDataAdapter da = new SqlDataAdapter() { SelectCommand = cmd };
// Load records to a DataSet
DataSet ds = new DataSet();
da.Fill(ds, "YourTableName");
var table = ds.Tables["YourTableName"];
// Add a new row with default value
table.Rows.Add();
// Reflect the change to database
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds, "YourTableName");
}
Also, if you've set an auto increment key like IDENTITY for MS SQL Server (https://msdn.microsoft.com/en-us/library/ms186775.aspx), this works when the new row is inserted.

Why doesn't DataSet create a new table in the database?

I have a small problem with creating a new table in a database. I create a new table in my DataSet object, but this table is not created in the physical database. The table is created only in cache. Why?
My code:
SqlCeConnection con = new SqlCeConnection(#"Data Source=C:\Projects\ConsoleApplication6\ConsoleApplication6\Data.sdf");
SqlCeDataAdapter sCEdata = new SqlCeDataAdapter("select * from [Cats]", con);
DataSet ds = new DataSet();
sCEdata.Fill(ds);
DataColumn ID = new DataColumn("ID", typeof(int));
ID.AllowDBNull = false;
ID.AutoIncrementSeed = 0;
ID.AutoIncrement = true;
ID.AutoIncrementStep = 1;
ID.ReadOnly = true;
ID.Unique = true;
DataColumn Name = new DataColumn("Name", typeof(string));
DataColumn Owner = new DataColumn("Owner", typeof(string));
DataColumn Note = new DataColumn("Note", typeof(string));
DataTable Cats2 = new DataTable("Cats2");
Cats2.Columns.AddRange(new DataColumn[]{ID, Name, Hozain, Note});
DataRow dr1 = Cats2.NewRow();
DataRow dr2 = Cats2.NewRow();
DataRow dr3 = Cats2.NewRow();
dr1["Name"] = "Pavel"; dr1["Owner"] = "Sergey"; dr1["Note"] = "Starii";
dr2["Name"] = "Gleb"; dr2["Owner"] = "Inga"; dr2["Note"] = "Tupaya";
dr3["Name"] = "Dusia"; dr3["Owner"] = "Olga"; dr3["Note"] = "Zlaya";
Cats2.Rows.Add(dr1);
Cats2.Rows.Add(dr2);
Cats2.Rows.Add(dr3);
dr2["Note"] = "Guzelle";
ds.Tables.Add(Cats2);
SqlCeCommandBuilder build = new SqlCeCommandBuilder(sCEdata);
sCEdata.Update(ds);
The Database only knows that is has to change something when you use a SQL command. You can create as many DataSets as you like, delete and insert data, but the Database stays the same until you tell it to change via a SQL command.

How to fill DataGridViewComboBoxColumn from a database table column without using datareader

Here is my code:
SqlDataAdapter GridDataAdapter = new SqlDataAdapter(query, con);
DataSet GridDataSet = new DataSet();
GridDataAdapter.Fill(GridDataSet, tbln);
dataGridView1.DataSource = GridDataSet;
DataGridViewComboBoxColumn dgvCB = new DataGridViewComboBoxColumn();
dataGridView1.DataMember = tbln;
Here, I want to fetch data for DataGridViewComboBox from a database table column.
How can I fill the DataGridViewComboBoxColumn without using a DataReader?
You already have the dataset filled, you could just iterate over the row collection if it contains the values that you want the DGVCombo to contain. So, the most straightforward way would be something along the lines of:
foreach(DataRow r in GridDataSet.Tables[0].Rows)
{
dgvCB.Items.Add(r["MyColumn"]);
}
Where .Tables[0] has the column("MyColumn") that you are looking for...
This should be what you need...
DataGridViewComboBoxColumn dgvCB = new DataGridViewComboBoxColumn();
dgvCB.Name = "lastname";
dgvCB.DataSource = tbln;
dgvCB.HeaderText = "Last";
//uncomment this to actually select the value in the combo box
//dgvCB.DataPropertyName = "lastname";
dgvCB.ValueMember = "lastname";
dataGridView1.Columns.Add(dgvCB);

datacoloumn in asp.net

I first want to add a database column in DataColumn..and then i have to add datacoloumn in datatable.. please guide me;
SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from exam_cyctstmarkdet",con);
DataColumn c = new DataColumn();
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
Is the bit you're missing
DataTable DT = new DataTable;
d2.Fill(DT);
DT.Columns.Add(c);
???
If you wish to create a DataTable, and add your own columns to it, you can try
DataTable dt = new DataTable("MyTable");
DataColumn col = dt.Columns.Add("parta", typeof(int));
You only have to keep a reference to the column if you plan to use it somewhere, else you can try
DataTable dt = new DataTable("MyTable");
dt.Columns.Add("parta", typeof(int));
I would suggest that you use the SqlDataAdapter to populate a DataSet, which will contain a DataTable. I assume you want to add the new column to this DataTable.
Here's how to do that:
SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from exam_cyctstmarkdet",con);
DataSet dst = new DataSet();
d2.Fill(dst); // now you have a populated DataSet containing a DataTable
DataTable dt = dst.Tables[0]; // I created this variable for clarity; you don't really need it
DataColumn c = new DataColumn();
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
dt.Add(c);
After you add the Column to the DataTable, the Column will be empty.
You can populate it in code.

Categories

Resources