I have two DataTables, DataTable1 sends some of its values to DataTable2. What I want to do is if DataTable1 values already exists in DataTable2, the values will not add to DataTable2.
I have here values from DataTable1
protected void ASPxGridView1_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e)
{
if (e.ButtonID != "ADD") return;
int id = e.VisibleIndex
int DeliveredQty = Convert.ToInt32(ASPxGridView1.GetRowValues(id, "Delivered Qty"));
int InventoryID = Convert.ToInt32(ASPxGridView1.GetRowValues(id, "InventoryID"));
Now I want to check the DataTable2 values if there's an existing InventoryID in the rows.
This is what I ended up with
int id = InventoryID;
DataTable dt = DataTable2;
DataRow[] dr = dt.Select(id.ToString());
if (dr == null)
{
"if it does not exist, values will be addded"
}
else
{
"prompt user that values exist"
}
What's the correct way to do this?
You can use Linq to check for a value in a DataTable column
bool valueExists = dt.AsEnumerable().Any(x => x.Field<string>("ColumnName") == "abcd");
Related
I want to get the value of a certain cell in the DataGrid of syncfusion.
like this:
<dataGrid:SfDataGrid x:Name="sfDataGrid">
</dataGrid:SfDataGrid>
C#:
Sfdatagrid\[i,j\] is useless.
How do I get the value of this cell.
https://help.syncfusion.com/winui/datagrid/overview The document
In this official document, I didn't find out how to get the value of this cell according to the index of rows and columns.
I want to get the value of this cell according to the index of the row and column.
Column A
Column B
Cell 1
Cell 2
Cell 3
Cell 4
For example, I want to get the value of this cell according to the index of the row and column.
How should I write code in c#
Your requirement to get the value of this cell according to the index of the row and column in SfDataGrid can be achieved by using View.GetPropertyAccessProvider.GetValue method in SfDatGrid. Please refer the below code snippet,
private void OnGetCellValueClicked(object sender, RoutedEventArgs e)
{
// Get the cell value for RowIndex = 5 and ColumnIndex = 2
int rowIndex = 5;
int columnIndex = dataGrid.ResolveToGridVisibleColumnIndex(2);
object record = null;
if (columnIndex < 0)
return;
var provider = this.dataGrid.View.GetPropertyAccessProvider();
var mappingName = dataGrid.Columns[columnIndex].MappingName;
var recordIndex = this.dataGrid.ResolveToRecordIndex(rowIndex);
if (this.dataGrid.View.TopLevelGroup != null)
{
var displayElement = this.dataGrid.View.TopLevelGroup.DisplayElements[recordIndex];
if (displayElement == null)
return;
if (displayElement is RecordEntry)
record = ((RecordEntry)displayElement).Data;
}
else
{
record = this.dataGrid.View.Records[recordIndex].Data;
if (record == null)
return;
}
var cellValue = provider.GetValue(record, mappingName);
//here display the cell value for RowIndex = 5 and ColumnIndex = 2
if (cellValue != null)
txtGetCellValue.Text = cellValue.ToString();
}
How to count the no of records in Gridview which have some particular data in a column
name result
======== ======
krishna pass
sanjay pass
ajay fail
out put needed in grid view - above Gridview already present,according to that grid i have to make another grid to count results
result no
====== =====
pass 2
fail 1
in data row bound , i calculated
protected void GVKeywordReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow pr = ((DataRowView)e.Row.DataItem).Row;
int oldPos = Convert.ToInt32(pr["oldposition"]);
int newPos = Convert.ToInt32(pr["newposition"]);
GVKeywordReport.HeaderRow.Cells[3].Text = txtfrmdate.Text;
GVKeywordReport.HeaderRow.Cells[4].Text = txtEndDate.Text;
GVKeywordReport.HeaderRow.BackColor = ColorTranslator.FromHtml("#B3B300");
e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#B3B300");
e.Row.Cells[5].BackColor = ColorTranslator.FromHtml("#FFFFFF");
if (oldPos == newPos)
{
e.Row.BackColor = ColorTranslator.FromHtml("#FF950E");
e.Row.Cells[6].Text = "No Change";
nc= nc+1;
}
else if (oldPos > newPos)
{
e.Row.BackColor = ColorTranslator.FromHtml("#FFFFCC");
e.Row.Cells[6].Text = "Improved";
imprv= imprv+1;
}
else
{
e.Row.BackColor = ColorTranslator.FromHtml("#FF0000");
e.Row.Cells[6].Text = "Decreased";
decrs=decrs+1;
}
// e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#7DA647");
}
txt_TargetReached.Text = "0";
txtDecreased.Text =Convert.ToString(decrs);
DataTable dtbind = new DataTable();
dtbind = objvehicleBAL.GetTaxdetails();
for (int i = 0; i < dtbind.Rows.Count; i++)
{
DateTime dt1 = DateTime.ParseExact(dtbind.Rows[i]["todate"].ToString(), "dd/MM/yyyy", null);
if (dt1 < ((DateTime.Now.AddDays(15))))
{
GVTax.DataSource = dtbind.Rows[i];
GVTax.DataBind();
}
}
I had written my conditions in if(). I want to bind only satisfied rows in grid. How can I write this?
You do not need to bind the Grid in loop on the Row of data table rather filter the DataTable by the condition you want and bind it once. You can get DataView from the data table and use its property DataView.RowFilter to apply the date filter.
dtbind = objvehicleBAL.GetTaxdetails(); //Filter the record in GetTaxdetails
DataView dv = dtbind.DefaultView; //or use DataView with RowFilter
dv .RowFilter = "todate = #" + DateTime.Now.AddDays(15).ToString() + "#";
GVTax.DataSource = dv;
GVTax.DataBind();
DataTable dtbind1 = objvehicleBAL.GetTaxdetails();
DataTable dtbind2 = new DataTable();
foreach (DataRow row in dtbind1.Rows)
{
DateTime dt1 = DateTime.ParseExact(row["todate"].ToString(), "dd/MM/yyyy", null);
if (dt1 < ((DateTime.Now.AddDays(15))))
dtbind2.Rows.Add(row);
}
}
GVTax.DataSource = dtbind2;
GVTax.DataBind();
No need to bind each row and call DataBind mehtod each time.
Just use the following:
protected void BindGrid()
{
DataTable dtbind = new DataTable();
dtbind=objvehicleBAL.GetTaxdetails();//get the rows filtered in SQL
if(dtbind!=null && dtbind.Rows.Count>0)//always check for null for preventing exception
{
GVTax.DataSource = dtbind;
}
GVTax.DataBind();
}
Hope this helps you!
You can use the Select method of DataTable along with a filtering expression, to get the rows which match your criteria. Then, bind it to to your GridView.
string filterExp = "todate < dateadd(day,15,getdate())";
var filtered = dtBind.Select(filterExp);
GVTax.DataSource = filtered ;
GVTax.DataBind();
You can create another datatable and fill the rows satisfying your condition in the second datatable and bind your gridview with second datatable (having filtered rows)
dttableNew = dttableOld.Clone();
foreach (DataRow drtableOld in dttableOld.Rows)
{
if (/*put some Condition */)
{
dtTableNew.ImportRow(drtableOld);
}
}
GVTax.DataSource = dtTableNew;
GVTax.DataBind();
This is my gridview output. The Column and row will be binded dynamically.
Anand R 4:4:18 NULL 6:34:52 8:25:16 NULL
Arunkumar S 8:37:31 NULL 9:1:42 8:48:27 NULL
Bharathi R 6:12:24 NULL 8:45:40 11:39:12 12:27:26
Gnanaguru V 6:32:20 NULL 5:35:56 3:50:20 NULL
Ilayaraja K 7:37:30 NULL 10:1:15 8:58:43 NULL
Imran Khan S 7:46:48 NULL 12:15:4 NULL NULL
I need output as if the cell value is greater than 8:30 hours means i need some color and below than 8:30 the cell should be in some color and for the null value the original remain same. So i need to loop through gridview rows and columns dynamically to check the condition. I am using following code in gridview rowdatabound but am not getting exact output..
foreach (GridViewRow row in gvshowreport.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
if (i != 0)
{
string value = row.Cells[i].Text;
if (value != " ")
{
string[] values = Regex.Split(value, ":");
int k = Convert.ToInt32(values[0]);
int m = Convert.ToInt32(values[1]);
int min = k * 60;
int add = min + m;
if (add <= 510)
{
e.Row.Cells[i].BackColor = System.Drawing.Color.LightGreen;
}
else
{
e.Row.Cells[i].BackColor = System.Drawing.Color.Red;
}
}
else
{
e.Row.Cells[i].BackColor = System.Drawing.Color.PaleGoldenrod;
}
}
}
}
First time my loop is running perfectly it taking the gridview first row and cell values. While next time loop running it again taking the same first row cell values. I think i have did mistake looping through the rows and columns...
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell cell in e.Row.Cells)
val = cell.Text;
}
I have use this code to achieve my result.. Thanks for everyone posting
as your code, you should replace "e.Row.Cells[i]" with variable "row"
We would change the color of cell in Row_dataBound Event of Gridview
When a single record bind with a gridView Row_dataBound event fires
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Your Cell Control Find, than change the color according to condition
}
}
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound%28v=vs.110%29.aspx
Please mark it answer if it helps you
I have a data table which has a "Total" column. I want to be able to get a specific rows "Total" not all rows.
public void maxValue()
{
string pass = (String)Session["name"];
DataTable table = (DataTable)Session["CocaCola"];
int total = table.AsEnumerable().Sum(r => r.Field<int>("Total"));
int totalAllowed = table.AsEnumerable().Sum(r => r.Field<Int32>("Total Allowed"));
if (total >= totalAllowed)
{
Label1.Text = "Total value exceeded the maximum of " + totalAllowed;
}
else if (total < totalAllowed)
{
Label1.Text = "Total value which is allowed :" + totalAllowed;
}
if (pass.Equals("Low"))
{
Label1.Text = "You are not allowed any assets at this Stage";
//SNS.Checked = false;
//TT.Checked = false;
//Music.Checked = false;
//SNS.Enabled = false;
//TT.Enabled = false;
//Music.Enabled = false;
}
}
As you can see my method works but add the column up which i dont want to do. How would i go about changing it?
You can do it this way
int yourTargetindex = 0; //Change this to get the value of your target element
int total =(from row in table.AsEnumerable()
select row.Field<int>("Total")).ElementAt(yourTargetindex);
//This will return the first value of "total" in the DataTable
You don't have to use linq. DataTable has built-in methods for this kind of operations:
var selectedTotal = table.Compute("sum(Total)", "columnX == 'x'");
This tell the table to calculate the sum of all Total cells in rows where columnX has the specified value.
Of course you can use linq. You would need to add a Where() before you calculate the sum.