How to use Datarow to retrieve data based on column name? I am trying to loop the db data from my first looping
//Trying to get data
DataRow dr = dsResult.Tables[1].Rows[0];
//trying to get data successful
//what i trying to achieve is to retrieve data from database based on rows index and
column name
for(int i =0; i <datagridview.Rows.Count ; i++){
string a = dr['ColumnName'].['RowsIndex'].toString(); //Failed
}
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow dr = dataTable.Rows[i]; //Where the RowIndex
string a = dr[0].ToString(); //Where the ColumnIndex or ColumnName
}
Try ?
dr.Rows[RowsIndex]['ColumnName'].ToString()
Have you tried removing the period after the 'columnName' specification, and putting rows[i]["columnName"] rather than [columnName][rows]?
Related
I have asked a question previously based on the errors of this code. However, after the suggestions given, there is no more error. However, the data from the row in queue table would not move to the the missedQueue table.
I'm not sure why it won't work :(
this is my code:
DataSet queue = DBMgr.GetDataSet("SELECT * FROM queue");
DataTable missedQueue = queue.Tables[0].Clone();
DataRow dr = queue.Tables[0].NewRow();
for (int i = 0; i < queue.Tables[0].Columns.Count; i++)
{
dr[queue.Tables[0].Columns[i].ColumnName] = queue.Tables[0].Rows[0][i];
}
missedQueue.Rows.Add(dr.ItemArray);
}
Your DataRow should be of missedQueue table and add the row inside loop like
DataRow dr = null;
for (int i = 0; i < queue.Tables[0].Columns.Count; i++)
{
dr = missedQueue.NewRow();
dr[queue.Tables[0].Columns[i].ColumnName] = queue.Tables[0].Rows[0][i];
missedQueue.Rows.Add(dr);
}
I'm having some problem while trying to set column value.
I'v had a dataTable which get some values from SQL and then im adding two new columns by :
dataTable.Columns.Add("dest", typeof(int));
dataTable.Columns.Add("amount", typeof(int));
Which works great but now i want to put 0 in every row in column name dest - and later user will edit this, and then i want to set amount value as
amount = all(this column is in dataTable before I add these 2 columns) + dest;
int columnNumber = 5; //Put your column X number here
for (int i = 0; i < yourDataTable.Rows.Count; i++)
{
yourDataTable.Rows[i][columnNumber] = "0";
}
You can use foreach too.
foreach (DataRow row in myDataTable.Rows)
//if (row["X"] has condition) // or if any condition
row["colName"] = row[colIndex] = "abc";
I usually don't post questions, and I know this answer is already out there somewhere but I'm not finding it for some reason. Basically, I have a datagridview that has a variable number of columns, and variable number of rows. So when I'm building it for the screen, I first add the columns using this code:
// Set column names
for (int i = 0; i < count; i++)
{
fieldName = fromFileFields[i];
dtaRecordDisplay.Columns.Add(fromFileFields[i}, fromFileFields[i});
}
Now, I would like to populate the rows in the view with something like this:
// Set row data
for (int i = 0; i < count; i++)
{
dtaRecordDisplay.Rows.Add(Row1, Row2, etc., etc., etc..........);
}
I'm having a hard time figuring out the loop to be able to add all rows of data at once. Does anyone have any ideas on how to accomplish that?
Thanks in advance!
In this case I would fill a DataTable with columns and rows to work with, and bind that to the grid.
DataTable dt = new DataTable();
// first add your columns
for (int i = 0; i < count; i++)
{
dt.Columns.Add(fromFileFields[i]);
}
// and then add your rows
for (int i = 0; i < count; i++)
{
var row = dt.NewRow();
// Set values for columns with row[i] = xy
dt.Rows.Add(row);
}
datagridview.DataSource = dt;
You need to make a DataTable, and bind that to your grid view. Then add the rows and colums to that, like Marc says:
https://stackoverflow.com/a/35604744/5882760
The reason for that is, that the columns of the grid view instance only contain information on how to display the data from the attached DataTable.
They contain the information about what columbs to display, in what order, etc.
Try following code, you can add rows to dataGridViews without out any data Sources or data table.
dataGridView1.AllowUserToAddRows = true;
dtaGridView1 row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
row.Cells[0].Value = "Your cell 0 Data";
row.Cells[1].Value = "Your cell 1 Data";
row.Cells[2].Value = "Your cell 2 Data";
………….
row.Cells[n].Value = "Your cell n Data";
dataGridView1.Rows.Add(row);
There is some data in my datagridview. I want to insert all that data at once into database from datagridview using LINQ. But, it couldn't be inserted.
Here is my code :
DetailTransaction dt = new DetailTransaction();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dt.TransactionID = labelID.Text;
dt.ProductID = dataGridView1.Rows[i].Cells[0].Value.ToString();
dt.Quantity = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
}
dc.DetailTransactions.InsertOnSubmit(dt);
dc.SubmitChanges();
Can anyone tell me what is the correct code?
You need to populate a collection of DetailTransaction and then use InsertAllOnSubmit like:
List<DetailTransaction > list = new List<DetailTransaction>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DetailTransaction dt = new DetailTransaction();
dt.TransactionID = labelID.Text;
dt.ProductID = dataGridView1.Rows[i].Cells[0].Value.ToString();
dt.Quantity = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
list.Add(dt);
}
dc.DetailTransactions.InsertAllOnSubmit(list);
dc.SubmitChanges();
With your current code, you will end up with a single row inserted in the database and that row will be holding the records of the last row in dataGridView1
I have a DataTable with columns of different types. What I want is a DataTable that has the same column names but all values are strings. That is, if this is the first:
Name Age
-----------
John 31
Alice 27
Marge 45
where Name is a String column and Age is an Int32 column, what I want is:
Name Age
-----------
John 31
Alice 27
Marge 45
where Name and Age are both string columns. The output table must contain the same values as the input table but every value must be converted to a string. Can anyone provide any insight on how one might go about doing this? I thought about maybe doing something like
foreach (DataColumn col in inputTable.Columns)
{
outputTable.Columns.Add(col.ColumnName, typeof(string));
foreach (DataRow row in inputTable.Rows)
{
...??
}
}
Or perhaps there is a better or more efficient approach? Any guidance would be greatly appreciated.
You can't modify a column type in DataTable if already has records. You can Clone DataTable and then modify column type in each column of cloned table. Later you can import rows.
DataTable dtClone = dt.Clone(); //just copy structure, no data
for (int i = 0; i < dtClone.Columns.Count; i++)
{
if (dtClone.Columns[i].DataType != typeof(string))
dtClone.Columns[i].DataType = typeof(string);
}
foreach (DataRow dr in dt.Rows)
{
dtClone.ImportRow(dr);
}
dtClone will have every column as of string and all the data from original table dt
You could use a built in method called toString().
foreach (DataColumn col in inputTable.Columns)
{
outputTable.Columns.Add(col.ColumnName, typeof(string));
foreach (DataRow row in inputTable.Rows)
{
outputTable.Columns.Add(row.toString());
}
}
in this code, you can convert whole dataset in to one string.
string ConvertDatasetToString(DataSet Ds)
{
string OUT = "";
for (int t = 0; t < Ds.Tables.Count; t++)
{
for (int r = 0; r < Ds.Tables[t].Rows.Count; r++)
{
for (int c = 0; c < Ds.Tables[t].Columns.Count; c++)
{
string s = Ds.Tables[t].Rows[r][c].ToString();
OUT += s;
}
}
}
return OUT;
}
if you want to convert one datatable into one string, you can omit the first for