I've a datatable, with column A, B, C. I've set column A's "is identity" property to true, but I can't add any row to the table now.
The code I'm trying is this:
dsA.dtA row = dsA.dtA.NewdtARow();
row.B = 1;
row.C = 2;
dsA.dtA.Rows.Add(row);
I'm getting NoNullAllowedException, but I don't understand why. Column A is PK as well. If I tried to set row.A = 5 (or any similiar) I'll get an error when I try to update the datatable saying "cannot insert explicit value for identity column in table when identity_insert is set to off"
How can I solve this issue? It's frustrating.
Do this way. Reference link
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
// Add the column to a new DataTable.
DataTable table = new DataTable("table");
table.Columns.Add(column);
DataRow oRow = table.NewRow();
table.Rows.Add(oRow);
Open the designer of the dataset xsd file and set the AutoIncrement, AutoIncrementSeed and AutoIncrementStep property of the column A in datatable for an existing column.
Try one of the these two:
Set field values:
row.A = null;
row.B = 1;
row.C = 3;
Add row to DataTable:
dtA.Rows.Add(null,1,2);
They are both the same just try any of them and it should get you going. Also remember that whenever you want to make a column auto-increment in DataTable then you have to insert null into it.
The way that I do it is
public void AppendtodtA(int num1, doubledouble1, string string1)
{
object[] RowArray = new object[] {null,(int)num1, (double) doubledouble1, (string) string1}
DataRow CurrentRow = dtA.NewRow();
CurrentRow.ItemArray = RowArray;
dtA.Rows.Add(CurrentRow);
}
use ItemArray property and leave a null in the place of the autoincremented column
AppendtodtA(MyNumber,MyDouble,MyString);
Then just call the method with your variables.
Related
I pull the data from sql database and fill datagridView1 with it:
dataGridView1.DataSource = bsour;
User will be able to edit value in 1 column, but possible values for that column are only Null, 0, 1 or 2 so to prevent from inputting incorrect value i decided to do a ComboBoxColumn:
DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn();
col1.Name = "il_normal_combo";
col1.HeaderText = "Normal";
col1.Items.Add("");
col1.Items.Add("0");
col1.Items.Add("1");
col1.Items.Add("2");
dataGridView1.Columns.Insert(5, col1);
The other now unnecessary column I'll hide with .Visible = false;
I want the ComboBoxColumn to display database values from that other column that will be hidden and i can't seem to get it to work.
What i have tried:
col1.ValueMember = "il_normal";
col1.DisplayMember = "il_normal";
Those don't work but without error message.
This doesn't work and produces error "Values are incorrect"
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells[5].Value = dataGridView1.Rows[i].Cells[7].Value;
}
I found similar question here
Change DataGridViewColumn to DataGridViewComboBoxColumn in bound DataGridView
but solution in that post didn't work for me. In ComboBoxColumn instead of number, I got System.Data.DataRows.(something) so i deduced that it doesn't know which column to use (even though there was only one in DataSource) so i added
col1.DisplayMember = "number";
And then it worked :-)
So i had to make a running balance column based on an existing data table.
I have the formulas and everything.
I would just like to know if it is possible to make another column in that data table and generically add data to it based on the result of the formula,
currently I loop through the table row by row and equate the last column, can I put the result in that same row?
Thanks everyone
DataColumn newColumn = new DataColumn("ColumnName", typeof(System.String)); // I considered the column datatype as string, u can give as u want
newColumn.DefaultValue = "Your Value";
dataTable.Columns.Add(newColumn);
We can add a new column with default value as above..
If you want add some other value means, go for for-loop or foreach..
foreach (DataRow DR in dataTable.Rows)
{
DR["ColumnName"] = "Your Value";
}
Yes, you can add a new column to the data table any time, and once added, can always set the value of the RunningBalance cell (column + row) to the value you calculated.
DataColumn runningBalanceColumn = myDataTable.Columns.Add("RunningBalance", typeof(Int64));
runningBalanceColumn[0] = RUNNING_BALANCE_FOR_FIRST_ROW;
// and so on
Also refer [MSDN][1]
// Create total column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
totalColumn.Expression = "Price+ Quantity";
// Add columns to DataTable.
...
table.Columns.Add(totalColumn);
Why do I get the exception that Column Name is not found for MyEntity as well as FullName Columns? Although I see the column names being displayed in UI.
InitializeComponent();
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
int rowIndex = dataGridView1.Rows.IndexOf(row);
myObject = dataGridView1.Rows[rowIndex].Cells["MyEntity"].Value as IEntityObject;
fileName = dataGridView1.Rows[rowIndex].Cells["FullName"].Value.ToString();
}
Because, infuriatingly enough, that datagridview column is not actually named the same as your DataTable column name. If you look at the column collection in the designer Properties window, you will see that is probably named something like "DataGridViewColumn4" or similar.
If you know the index, you should use that, or rename the columns to the DT column names.
Incase it is not completely obvious, programmatically adding a checkbox to your datagridview with the name set should look like below:
DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
col.Width = 90;
col.HeaderText = "Sync To Vend?"; //Header cell text
col.Name = "SyncVendBox"; //This is the important part
ProductGrid.Columns.Insert(2, col);
And if you want the checkbox ticked:
for (int i = 0; i < ProductGrid.Rows.Count; i++)
{
ProductGrid.Rows[i].Cells["SyncVendBox"].Value = true;
}
I have a issue with my code in C#. I have set up a couple of DataTables with a primary key assigned to each of them. what I want to do is to retrieve a single row from a single column.
Lets say I have this code:
DataColumn Pcolumn = new DataColumn();
DataColumn[] key = new DataColumn[1];
Pcolumn.DataType = System.Type.GetType("System.Double");
Pcolumn.ColumnName = "length";
key[0] = Pcolumn;
table6F.Columns.Add(Pcolumn);
table6F.Columns.Add("Area", typeof(double));
table6F.Columns.Add("load", typeof(double));
table6F.Columns.Add("weigth", typeof(double));
table6F.PrimaryKey = key;
table.Rows.Add(6.0, 14.0, 17.8 , 11.0 );
table.Rows.Add(7.0, 16.2 , 20.7 , 16.0 );
And I want to retrieve the the "load" for the second row (20.7), I would like to search for 7.0, primary key column, in the Table. I dummy tested to do like this, just to test:
Object oV;
double load;
//Get an Table object given the specific row number, this dummy i always set to 0.
// Given Column
oV = table.Rows[0]["load"];
load = Convert.ToDouble(oV.ToString());
Is there a similar way to extract using the Primary key?
You can retrieve a row from a DataTable based on its primary key using the DataRowCollection.Find method. In your case it would be:
DataRow matchingRow = table.Rows.Find(7.0D);
double value = (double)matchingRow["load"];
You can use Find method to find a row using primary Key
DataRow row = dataTable.Rows.Find("your key");
if(null != row)
{
string value = row["ColumnName"];
}
How do I add a new DataColumn to a DataTable object that already contains data?
PseudoCode
//call SQL helper class to get initial data
DataTable dt = sql.ExecuteDataTable("sp_MyProc");
dt.Columns.Add("NewColumn", type(System.Int32));
foreach(DataRow row in dr.Rows)
{
//need to set value to NewColumn column
}
Just keep going with your code - you're on the right track:
//call SQL helper class to get initial data
DataTable dt = sql.ExecuteDataTable("sp_MyProc");
dt.Columns.Add("NewColumn", typeof(System.Int32));
foreach(DataRow row in dt.Rows)
{
//need to set value to NewColumn column
row["NewColumn"] = 0; // or set it to some other value
}
// possibly save your Dataset here, after setting all the new values
Should it not be foreach instead of for!?
//call SQL helper class to get initial data
DataTable dt = sql.ExecuteDataTable("sp_MyProc");
dt.Columns.Add("MyRow", **typeof**(System.Int32));
foreach(DataRow dr in dt.Rows)
{
//need to set value to MyRow column
dr["MyRow"] = 0; // or set it to some other value
}
Only you want to set default value parameter. This calling third overloading method.
dt.Columns.Add("MyRow", type(System.Int32),0);
Here is an alternate solution to reduce For/ForEach looping, this would reduce looping time and updates quickly :)
dt.Columns.Add("MyRow", typeof(System.Int32));
dt.Columns["MyRow"].Expression = "'0'";
Try this
> dt.columns.Add("ColumnName", typeof(Give the type you want));
> dt.Rows[give the row no like or or any no]["Column name in which you want to add data"] = Value;