ASP.NET DATA TABLE Traverse Problem - c#

I am getting the Data table a output from my DataAccess Layer.
In My Datatable I am getting users Name,Number,Qualification
I want to assign a users name,number,qualification to a textbox for a particular user id,
How can i do that.
Help

Suppose you got datatable dt from the DAL
Then
var row = from t in dt
where t["userId"]='userid'
select t;
since you got row related to a user now you can use it to assign to the textboxs
txtName.Text = row["Name"]

assuming the datatable has 1 row:
DataRow row = table.Rows[0];
textbox1.text = row["Name"];
textbox2.text = row["Number"];
and so on
if there are multiple rows in the datatable, you need to select the row with that particular used id
DataRow row = table.Select("ID=" + UserID.ToString());

You have to pay attention to NULL values and number of rows.
if (table.Rows.Count == 1)
{
DataRow row = table.Rows[0];
NameTextBox.Text = row.IsNull("name") ? string.Empty : row["name"];
NumberTextBox.Text = row.IsNull("number") ? string.Empty : row["number"];
}
else
{
// Deal with no rows from DL
}
Using ternary operator makes you sure, you delete content of TextBoxes in case you reload row within already filled up page.
Also you may consider to used typed dataset, and access to columns will be generated by xsd.exe.

Related

Get a row from a datatable in C# Visualstudio and then store the row in a datarow

I have a dataset in c# visual studio. I have one of column values of the one of the rows. I need to select that row using that value and store it in a datarow so that I can delete it. So how can I get the row of the datatable from one of the column values. I was trying something like this:
DataRow rowtobeselected = TableName["SerialNumber = 156"];
But as you know this is wrong. Also I need to get another colum value
from this row. For eg I need to get the value from the column date.
string Date = rowtobeselected.date;
I want to do something like this. So how to do it correctly. Thank you.
DataTables support a SQL like Select syntax:
https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable.select?view=net-5.0
DataRowView row = (DataRowView)OtherTable.SelectedItem;
string serialnumbr = row.Row["SerialNumber"].ToString();
DataRow[] result = TableName.Select("SerialNumber =" + serialNumbr);
DataRow dr = result.FirstOrDefault();
if (dr != null)
{
DateTime dt = DateTime.Parse(dr["Date"].ToString());
}

How can I send the selected rows of a DataGridView to a Datatable, BUT ignoring the first column?

Hellou to everyone.
I would love the assistance from you guys.
My problem is like this:
I have this datagridview which I fill using datasource (a query to a db table). I only created manually one column which contain a checkbox. When the binding is complete I can click the checkboxes to select the row and I can select multiples rows this way.
I want to put the selected rows into a datatable (so I can read it and do some sql queries with the information on it) BUT when I read the selected rows to put them inside a datatable I need to skip the checkbox column (the first one).
How can I achieve this?
Okay, for future reference, I'm gonna post here how I fixed it.
DataTable dt = new DataTable();
if (dgvPedidos.SelectedRows.Count > 0)
{
foreach (DataGridViewColumn column in dgvPedidos.Columns)
dt.Columns.Add();
int i = 0;
foreach(DataGridViewRow row in dgvPedidos.Rows)
{
if (Convert.ToBoolean(dgvPedidos.Rows[i].Cells["chkPedido"].Value) == true)
{
dt.Rows.Add();
dt.Rows[i][1] = row.Cells[1].Value.ToString();
dt.Rows[i][2] = row.Cells[2].Value.ToString();
dt.Rows[i][3] = row.Cells[3].Value.ToString();
dt.Rows[i][4] = row.Cells[4].Value.ToString();
dt.Rows[i][5] = row.Cells[5].Value.ToString();
dt.Rows[i][6] = row.Cells[6].Value.ToString();
dt.Rows[i][7] = row.Cells[7].Value.ToString();
dt.Rows[i][8] = row.Cells[8].Value.ToString();
dt.Rows[i][9] = row.Cells[9].Value.ToString();
i++;
}
}
dt.Columns.RemoveAt(0);

how can i get the value of the specific column of a table?

How can i read the value from every row of a specific column?
I have a table shown here Table Image, i want to get the model name from the "Model" column and i have a database and "SELECT" query behind depending on the "Model" that is shown, it will show the quantity of the "Model" can someone help me?
here is a sample code of my .cs
foreach (GridViewRow row in gv1.Rows)
{
for(int i = 0; i<gv1.Columns.Count;i++)
{
string model = row.Cells[i].Text;
string quan = row.Cells[3].Text;
string ams = row.Cells[4].Text;
}
}
Note:-
data shown in the table are already in the database.
i have 2 seperate View tables for the Models and the Quantity.
If i understand well what you want it's clearly not the best approach, but i will try to help you.
In my opinion you have to get the quantity value before the display of your dataTable, inside this DataTable, but anyway i think this is the type of code that you requested :
//My dataTable to test , with 3 columns
DataTable dt = new DataTable();
dt.Columns.Add("Model");
dt.Columns.Add("Other");
dt.Columns.Add("QuantityFromDB");
//Add some test data in my dataTable
for(int i=0;i<15;i++)
{
DataRow dr = dt.NewRow();
dr[0] = i*2+"";
dr[1] = "XX data XX";
dt.Rows.Add(dr);
}
//Foreach row in your datatable ( your data )
for(int y=0;y<dt.Rows.Count;y++)
{
//Get the value of the current "Model" Column value
string currentModel = dt.Rows[y]["Model"].ToString();
//make your request in the db to get the quantity
int quantityfromdb = 50; //REQUEST
//Update the value of the QuantityFromDB column for this model row
dt.Rows[y]["QuantityFromDB"] = quantityfromdb;
}
You read the value of a specific row, and edit the value of it's other column.
EDIT :
If you want to work directly on the gridview, you can moove your code in the row_databound event of your gridview.
See doc : https://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.110).aspx

How to copy all the rows in a datatable to a datarow array?

I have two tables:
tbl_ClassFac:
ClassFacNo (Primary Key)
,FacultyID
,ClassID
tbl_EmpClassFac:
EmpID, (Primary Key)
DateImplement, (Primary Key)
ClassFacNo
I want to know all the Employees who are on a specific ClassFacNo. ie. All EmpID with a specific ClassFacNo... What I do is that I first search tbl_EmpClassFac with the EmpID supplied by the user. I store these datarows. Then use the ClassFacNo from these datarows to search through tbl_ClassFac.
The following is my code.
empRowsCF = ClassFacDS.Tables["EmpClassFac"].Select("EmpID='" + txt_SearchValueCF.Text + "'");
int maxempRowsCF = empRowsCF.Length;
if (maxempRowsCF > 0)
{
foundempDT = ClassFacDS.Tables["ClassFac"].Clone();
foreach (DataRow dRow in empRowsCF)
{
returnedRowsCF = ClassFacDS.Tables["ClassFac"].Select("ClassFacNo='" + dRow[2].ToString() + "'");
foundempDT.ImportRow(returnedRowsCF[0]);
}
}
dataGrid_CF.DataSource = null;
dataGrid_CF.DataSource = foundempDT.DefaultView;
***returnedRowsCF = foundempDT.Rows;*** // so NavigateRecordsCF can be used
NavigateRecordsCF("F"); // function to display data in textboxes (no importance here)
I know the code is not very good but that is all I can think of. If anyone has any suggestions please please tell me. If not tell me how do I copy all the Rows in a datatable to a datarow array ???
"How to copy all the rows in a datatable to a datarow array?"
If that helps, use the overload of Select without a parameter
DataRow[] rows = table.Select();
DataTable.Select()
Gets an array of all DataRow objects.
According to the rest of your question: it's actually not clear what's the question.
But i assume you want to filter the first table by a value of a field in the second(related) table. You can use this concise Linq-To-DataSet query:
var rows = from cfrow in tbl_ClassFac.AsEnumerable()
join ecfRow in tbl_EmpClassFac.AsEnumerable()
on cfrow.Field<int>("ClassFacNo") equals ecfRow.Field<int>("ClassFacNo")
where ecfRow.Field<int>("EmpId") == EmpId
select cfrow;
// if you want a new DataTable from the filtered tbl_ClassFac-DataRows:
var tblResult = rows.CopyToDataTable();
Note that you can get an exception at CopyToDataTable if the sequence of datarows is empty, so the filter didn't return any rows. You can avoid it in this way:
var tblResult = rows.Any() ? rows.CopyToDataTable() : tbl_ClassFac.Clone(); // empty table with same columns as source table

Updating datagridview in Window application

I have a table with 6 columns ID,A,B,C,D and E. ID is primary key.
An third party application keep updating Column D and E and also
adding new rows to the table.
I want to display this table to a datagridview control. I am trying to update it each after 5 sec. So far i have tried:
Table may have 3-100 records.
//dt is a DataTable with new data
//dtPos is DataTable currently bind to datagridview
foreach (DataRow r in dt.Rows)
{
dtPos.DefaultView.RowFilter = "ID = '" + r["ID"].ToString() + "'";
if (dtPos.DefaultView.Table.Rows.Count > 0)
{
dtPos.DefaultView.Table.Rows[0]["D"] = r["D"];
dtPos.DefaultView.Table.Rows[0]["E"] = r["E"];
}
else
{
dtPos.Rows.Add(r);
}
}
This did not seem to work as expected. any better idea?
hi i suggest you to do above your task in gridview itself so you can do it instantly insteed of doing it in table and then assigning it back to the datagridview.
dgv have add row method so you can add row in dgv directly .
use like this when u need to add new row
dgv.Rows.Add(1,2,3,4,4,5);
and when u need to change the value of the cell then
dgv.rwos[roindex].cells[cellindex].value = xxx; //your new value;
this can help u ....

Categories