I have a grid view which contains 10 rows and 3 columns.. Now i want to loop through all rows and all columns of the gridview and want to add them to a datatable..
DataRow row;
int rowscount = gv.Rows.Count;
int columnscount = gv.Columns.Count;
for (int i = 0; i < rowscount; i++)
{
for (int j = 0; j < columnscount; j++)
{
row = empTable.NewRow();
row["a"] = gv.Rows[i][column1].Tostring();
row["b"] = gv.Rows[i][column2].ToString();
MynewDatatable.Rows.Add(row);
}
}
gv - my gridview
Now my question is , Can i get the value of all columns of all rows in gv to my new datatable.. I dont know whether my loop is correct or not... I am using this datatable for a bulkcopy function...
Your code above is creating a new Row for every cell in the GridView.
within each row of the GridView your code is assigning every value in the row to the same column, Emp_Name.
Corrected, I think:
int rowscount = gv.Rows.Count;
int columnscount = gv.Columns.Count;
for (int i = 0; i < rowscount; i++)
{
// Create the row outside the inner loop, only want a new table row for each GridView row
DataRow row = empTable.NewRow();
for (int j = 1; j < columnscount; j++)
{
// Referencing the column in the new row by number, starting from 0.
row[j - 1] = gv.Rows[i][j].Tostring();
}
MynewDatatable.Rows.Add(row);
}
EDIT: You've been editing the source in your question, so my answer may grow out-of-date. :)
Your loop is incorrect, all lists start at 0. But you start at 1 when looping through the columns.
The JMD answer is correct, but depending on your use-case, maybe databinding the DataSource of your grid view to your DataTable would be a better approach.
Try this code:
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
string selectedempid = dataGridView1.SelectedRows[0].Cells["Deptno"].Value.ToString();
{
SqlCommand cmd = new SqlCommand("delete from Test_dept where Deptno=" + selectedempid, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("deleted");
}
}
Related
I'm trying to get a selected datagridview row to a datatable.
With the following code I am able to send the selectedrow over, however when trying to send a second row to the datatable, it adds an empty row but overwrites the first row in the datatable.
for (int i = 0; i < dataGridview.SelectedRows.Count; i++)
{
DT1.Rows.Add();
for (int j = 0; j < dataGridview.Columns.Count; j++)
{
DT1.Rows[0][j] = dataGridview.SelectedRows[i].Cells[j].Value;
}
}
As expected it enters the row on index 0, so i tried the folowing to get to the last row
for (int j = 0; j < dataGridview.Columns.Count; j++)
{
int rowcount = DT1.Rows.Count;
DT1.Rows[rowcount][j] = dataGridview.SelectedRows[i].Cells[j].Value;
}
This gives me an error that the row does not exist, what should be the solution to add a second row instead of replacing?
You've got other problems in your code. Namely, if your user selects the same rows in different calls to this code, you will insert duplicates.
Fix rowcount, changing it to rowcount-1. Then review your design. Come up with an approach to prevent duplicate insertions (unless you really want that).
You have a problem in rowcount
Rowcount gives you total no of rows but rowindex always starts with 0
ie rowindex = rowcount-1
use this
for (int j = 0; j < dataGridview.Columns.Count; j++)
{
int rowcount = DT1.Rows.Count;
DT1.Rows[rowcount-1][j] = dataGridview.SelectedRows[i].Cells[j].Value;
}
DT1.Rows[i][j] = dataGridview.SelectedRows[i].Cells[j].Value,ToString();
Try this! Your outer loop is already keeping track of the rows in your tables so there's no need to mess with the row count.
I wanna show all my DataSource rows on a DataGridView, but not as rows but as columns of a row. Each 12 items retrieved, I wanna insert a new row on the DataGridView and populate this new row with more items from the DataSource (12 at each row).
My DataSource retrieves just one item each, and using it directly with the DataGridView is working nicely, but shown a different row for each item.
Any hints?
Thanks to #SriramSakthivel.
private void AddToList(DataTable dt)
{
possibleWords = 0;
// Cleans the data grid view
WordList.DataSource = null;
WordList.Refresh();
// Let's transform the original data table onto another, changing rows by columns
DataTable table = new DataTable();
for (int i = 0; i < 10; i++)
{
table.Columns.Add(Convert.ToString(i));
}
DataRow r;
int col = 0;
//for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
for (int j = 0; j < dt.Rows.Count; j++)
{
if (col >= 10)
{
table.Rows.Add(r);
col = 0;
r = table.NewRow();
}
r[col++] = (dt.Rows[j][0]).ToString().ToUpper();
possibleWords++;
}
table.Rows.Add(r);
}
// Puts the new data table as datasource of the word list
DataView dv = table.DefaultView;
WordList.DataSource = dv;
if (possibleWords == 0)
return;
WordList.Columns[0].DefaultCellStyle.BackColor = Color.WhiteSmoke;
WordList.ColumnHeadersVisible = false;
WordList.RowHeadersVisible = false;
}
I have a data table that contains the data like below.
I want to display it in my Grid View like Following. It is actually the transpose of above table and one additional Row added for Viewing Product Details that will be a link button.
Can you please help me how can I achieve the following requirement in ASP.net using C#.
Many Thanks,
Awais Afzal.
Assuming that you table is a DataTable, you could use such an extension to reorder it:
public static DataTable Pivot(this DataTable tbl)
{
var tblPivot = new DataTable();
tblPivot.Columns.Add(tbl.Columns[0].ColumnName);
for (int i = 1; i < tbl.Rows.Count; i++)
{
tblPivot.Columns.Add(Convert.ToString(i));
}
for (int col = 0; col < tbl.Columns.Count; col++)
{
var r = tblPivot.NewRow();
r[0] = tbl.Columns[col].ToString();
for (int j = 1; j < tbl.Rows.Count; j++)
r[j] = tbl.Rows[j][col];
tblPivot.Rows.Add(r);
}
return tblPivot;
}
and set it as new DataSource:
dataGridView1.DataSource = oldDataTable.Pivot();
I want to insert datarow value in datatable particular row. Am having a datatable with values and then i want to overwrite the particular row value using c#.
my partial code id here:
for (int j = 0; j < DT.Rows.Count; j++)
{
row = DT2.NewRow();
row["Employee ID"] = Convert.ToString(DS.Rows[j]["fldempid"]);
row["Employee Name"] = Convert.ToString(DS.Rows[j]["fldempname"]);
string leavehstry = Convert.ToString(DS.Rows[j]["fldleavehistory"]);
string[] textarray1 = leavehstry.Split('-');
char[] delimiterChars1 = { ':' };
for (int i = 1; i <= textarray1.Length - 1; i++)
{
string[] words = textarray1[i - 1].Split(delimiterChars1);
string value = words[0].ToString();
string value1 = words[1].ToString();
row[value] = value1;
ivalue = i;
}
//DT2.Rows[j].Delete(); //I want to insert (or) overwrite the row value at j th position.
//DT2.Rows.Add(row);
//DT2.Rows[j].AcceptChanges();
}
please help me to insert it..
You can insert a DataRow in a DataTable at a specified index location by using the InsertAt method.
You might do this in place of your three commented out lines:
DT2.InsertAt(row, j);
If EmployerID is an unique value, then compare the DataColumn "EmployerID" value with EmployerID and Assign the datarow values as you want by using for loop.
In the DataTable i have n number rows want add those rows in array?
DataTable tbl = new DataTable();
tbl.Rows
Rows is array of your records...
The code below stores all the information in a datatable in a double array of strings. The code should be plug-and-play, just make sure you swap out "dataTable1" with the name of your actual DataTable.
string[,] stringArray = new string[dataTable1.Rows.Count, dataTable1.Columns.Count];
for (int row = 0; row < dataTable1.Rows.Count; ++row)
{
for (int col = 0; col < dataTable1.Columns.Count; col++)
{
stringArray[row, col] = dataTable1.Rows[row][col].ToString();
}
}
To access the first item out of stringArray, simply use call the code below, using the indices as you would for a normal array.
stringArray[0, 0]