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.
Related
I currently have program that checks a date that is selected in MonthCalendar and uses it to search through the first column [date] for a matching value before performing a function. This function is to check the rest of the columns among the same row for a certain Value and then returning the column headers that have that Value. How do I do this?
private void button1_Click(object sender, EventArgs e)
{
string date = monthCalendar1.SelectionRange.Start.ToShortDateString();
string CSVFilePathName = #"pathname.csv";
string[] Lines = File.ReadAllLines(CSVFilePathName);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt5 = new DataTable();
for (int i = 0; i < Cols; i++)
dt5.Columns.Add(Fields[i].ToLower(), typeof(string));
DataRow Row;
for (int i = 1; i < Lines.GetLength(0); i++)
{
Fields = Lines[i].Split(new char[] { ',' });
Row = dt5.NewRow();
for (int f = 0; f < Cols; f++)
Row[f] = Fields[f];
dt5.Rows.Add(Row);
}
for (int i = 0; i < 29; i++)
{
Object o = dt5.Rows[i]["date"];
if (o.ToString() == date)
{
(INSERT CODE HERE TO CHECK REST OF COLUMNS ON SAME ROW AND RETURN COLUMN HEADER)
}
}
}
Hi I have now attached the code
You can use Microsoft Excel Interop or ClosedXML to read an excel file and then search for whatever you want.
You've said that you already have a program that does something. Can you post the code or whatever you've tried so far? It'll help us help you better.
I have two data tables
DataTable dt1=new DataTable();
DataTable dt2=new DataTable();
I want to update one of the column's values depending on other datatable column value.
Eg:
dt1 contains columns [setFamilyno] ["HouseNo"] ["Surname"]
dt2 contains the same columns
I want to update [setFamilyno] column of dt1 DataTable.
if dt1["HouseNo"] ["Surname"] is equal to dt2["HouseNo"] ["Surname"] then set the value 1 of dt1 DataTable for all such matches in dt1 table. And for next row from dt2["HouseNo"] ["Surname"] is equal to dt1["HouseNo"] ["Surname"] then set value for [setfamilyno]=2 for all such matches..
Try to create a DataRelation with a ForeignKeyConstraint. The help will be enough to know how to do it.
DataView view = new DataView(Datatble);
int viewcount = view.Count;
DataTable distinctValues = view.ToTable(true, "SurNameEnglish", "HouseNumber");
int distinctcount = distinctValues.Rows.Count;
int cnt = 1;
for (int j = 0; j < distinctcount; j++)
{
string surname = distinctValues.Rows[j]["SurNameEnglish"].ToString();
string Housenumber = distinctValues.Rows[j]["HouseNumber"].ToString();
for (int i = 0; i < viewcount; i++)
{
if (Datatble.Rows[i]["SurNameEnglish"].Equals(surname) && Datatble.Rows[i]["HouseNumber"].Equals(Housenumber))
{
Datatble.Rows[i]["Family"] = cnt;
Datatble.AcceptChanges();
}
}
cnt++;
}
}
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]
I have datatable table like
id name address phoneno
1 abc kfjskl 798798
2 bcd kjdsfl 808909
3 899009
fjsh kjllkjl
5 jfkd
And I am displaying this value in the datagridview by code
dataGridView1.ColumnCount = Table.Columns.Count;
dataGridView1.RowCount = Table.Rows.Count;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
dataGridView1[j, i].Value = Table.Rows[i][j].ToString();
}
}
Now I don't want to display row that has some missing value like If I will do that then the datagridview will look like
1 abc kfjskl 798798
2 bcd kjdsfl 808909
How can I do that ?
This will do what you need (I think):
dataGridView1.ColumnCount = Table.Columns.Count;
for (int i = 0; i < Table.Rows.Count; i++)
{
bool valueMissing = false;
DataGridViewRow row = new DataGridViewRow();
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
if (Table.Rows[i][j].ToString() == "")
{
valueMissing = true;
break;
}
else
{
row.Cells[j].Value = Table.Rows[i][j];
}
}
if (!valueMissing)
{
dataGridView1.Rows.Add(row);
}
}
This may need to be modified to check for null values in Table.Rows[i][i] (in addition to just checking for empty string values).
I think your best choice is to use a DataView between the DataGridView and the DataTable.
The DataView allows you to filter values using and expression without affecting to the original DataTable. To filter all empty columns you can create a view with this expression:
DataView view = new DataView(table);
view.RowFilter = "ISNULL(id, '') <> '' AND ISNULL(name, '') <> '' AND ISNULL(address, '') <> '' AND ISNULL(phoneno, '') <> ''";
The ISNULL function replaces a column value whit the specified string, if the column value is null. So the filter is replacing every NULL value with empty strings, so excluding NULL or Empty columns.
Then you can use this view to assing values to the grid, instead of using the datatable:
dataGridView1.ColumnCount = view.Columns.Count;
dataGridView1.RowCount = view.Rows.Count;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
dataGridView1[j, i].Value = view.Rows[i][j].ToString();
}
}
Also instead of using loops to provide values to the grid, you can simply use databinding. It's much simpler:
dataGridView1.DataSource = view;
Hope it helps!
I'm not sure if I understand, but you could possibly use DataTable.Select to filter out any rows with missing values before you insert them in the datagrid.
Or otherwise, instead of using a for loop for the rows, just use a foreach loop on the rows in the table and add the rows one by one with dataGridView.Rows.Add(newRow).
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");
}
}