Remove link text in Datagridview - c#

I have a DataGridView which is populated from a list. I then Have defined a link column in the last column which is not DataBound saying View, how do I remove this link text if a condition is met. I have the below:
dgvReport.AutoGenerateColumns = false;
dgvReport.DataSource = queries;
//locals
Int32 lastColumnIndex = this.dgvReport.Columns.Count - 1;
//hide reads links for the null queries
for (int i=0; i < queries.Count; i++) {
if (queries[i].SomeID == null || queries[i].AnotherID == null) {
this.dgvReport.Rows[i].Cells[lastColumnIndex].Value = "";
}
}
This is not working and my column text still shows View. Any ideas?
Edit: lastColumnIndex is 24 which is indeed the last column index

You might try
View.PlainTextBehaviour = true;
It will remove the link and show it as plain text

Related

Finding a cell in devexpress datagrid

I'm new to DevExpress GridControl.
I need to find a particular cell in the grid and then do something with it's value.
How do I go about this please?
In the grid's Loaded method, I tried using myGrid.FindRowByValue("ProductType","ABC"), but always gives a negative number.
Thanks.
Here is code you can try
for (int i = 0; i < gridView1.DataRowCount; i++) {
object b = gridView1.GetRowCellValue(i, "FieldName");
if (b != null && b.Equals(<someValue>)){
gridView1.FocusedRowHandle = i;
return;
}
}
you can go to this link for more details.
https://www.devexpress.com/Support/Center/Question/Details/Q132599/get-row-by-cell-value
Unlike XtraGrid, the DXGrid for WPF does not provide the DataRowCount property - that is why we suggested checking the GridControl's ItemsSource. On the other hand, our grid has the VisibleRowCount property, which will be useful in some scenarios.
To accomplish this task, iterate through visible grid rows manually as shown below.
void MoveFocusToLast(GridControl grid, string fieldName, object value) {
for (int i = grid.VisibleRowCount - 1; i >= 0; i--) {
var handle = grid.GetRowHandleByVisibleIndex(i);
var currentValue = grid.GetCellValue(handle, fieldName);
if (currentValue != null && currentValue.Equals(value)) {
grid.View.FocusedRowHandle = handle;
return;
}
}
}
Grid also provides the FindRowByValue method, which allows you to
find a row by a specific cell value. This method returns the handle of
the corresponding row, and you can make that row visible by setting
the FocusedRowHandle property or calling ScrollIntoView. I
have prepared a sample demonstrating this approach.
See Also:
Traversing Rows
Find Row
Get RowHandle from a cell value

ASP.NET C# DataTables replace specific value in specific columns in Table

I have a problem with checking the value of an specific Column in a DataTable.
My DataTable is coming from a sql query and it has in this case 8 columns.
My 3rd column is a column called Status. In the Database status is going to be saved as numbers. 1 = "open", 2 = "in work", 3 = "closed" .
Now I'm iterating my whole table until this column and I want to check whether the value is 1 2 or 3 and replace the 1 2 or 3 with the corresponding status name. (I'm iterating, because there will be more columns which are going to be modified, so not only the column 2 in every row, but also 4,5 - but they are going to be same way like column 2. So if I understand one, I can do the rest similar).
But I don't know how to check the value, I have tried the .contains and equals method.
Here is my code:
//for each row
for (int i = 0; i < dt.Rows.Count; i++)
{
//search colums
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j == 2)
{
if (dt.Columns[j].Equals("1") == true)
{
// dt.Columns[j].value should be replaced with "open";
}
if (dt.Columns[j].Equals("2") == true)
{
// dt.Columns[j].value should be replaced with "in-work";
}
if (dt.Columns[j].value == "3")
{
// dt.Columns[j].value should be replaced with "closed";
}
}
}
So how could I check the value of the Column and replace with a string?
Thanks in advance!
You can try checking with this:
if(dt.Rows[i][j].ToString() == "1")
And replace with this:
dt.Rows[i][j] = "Open";
Since dt.Columns[j] is a DataColumn convert it to string:
dt.Columns[j].ToString()
and then you can do
dt.Columns[j].ToString() == "1"
To rename your column just use the property Caption
var col = dt.Columns[j];
col.Caption = "in-work";

Clarification on how to use FirstDisplayedScrollingRowIndex

Just posted before and need clarification on a property. (Note I know this question is similar to others and hence I tried looking elsewhere for the solution but couldn't find the exact one for this situation).
I'm rather new to programming and don't know how to make a DataGridView scroll to the row selected by a user. I tried using FirstDisplayedScrollingRowIndex but am getting the following error:
Error: Cannot implicitly convert type 'System.Windows.Forms.DataGridViewRow' to 'int'
Which occurs when I try to bring the selected row by the user to the top of the datagridview:
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i]
Here is the full code:
String searchVal = textBox1.Text;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value != null && dataGridView1.Rows[i].Cells[0].Value.ToString().Contains(searchVal))
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i];
dataGridView1.Update();
}
}
According to the documentation, FirstDisplayedScrollingRowIndex:
Gets or sets the index of the row that is the first row displayed on the DataGridView.
You already have the index stored in i ... try just using that:
dataGridView1.FirstDisplayedScrollingRowIndex = i;
To address your second question in the comments, here's how you can find the first exact match (if any):
var selectedRow = dataGridView1.Rows.Cast<DataGridViewRow>()
.FirstOrDefault(x => Convert.ToString(x.Cells[0].Value) == searchVal);
if (selectedRow != null)
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[i];
dataGridView1.Update();
}
Just use i instead of dataGridView1.Rows[i]
dataGridView1.FirstDisplayedScrollingRowIndex = i;

How to remove empty row from gridview

I have a problem in window application. When I insert a record and display the records in a gridview, the gridview automatically makes one empty row. And I also use
dataGridView1.AllowUserToAddRows = false;
Please help me with an alternative solution to get rid of the empty row.
Default DGV will have a blank row at the bottom to enable user adding a new row, by setting dataGridView1.AllowUserToAddRows = false; will disable the blank row.
You may delete the blank rows manually like this:
for (int i = 1; i < dataGridView1.RowCount - 1; i++)
{
Boolean isEmpty = true;
for(int j=0;j<dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value.ToString() != "" )
{
isEmpty = false;
break;
}
}
if (isEmpty)
{
dataGridView1.Rows.RemoveAt(i);
i--;
}
}
HTH.
Are you sure you don't have a blank record in your datasource? another way you could check this is to add an if statment on to the OnRowDataBound to check the item index which if 0 (or some other way to identify the blank row) then set the row's Visible = false

How do I pull data from a javascript populated table?

I am trying to pull data from my populated javascript table. How do I pull the value from a javascript row? I am using
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
//This is where I am having trouble
var chkboxIndicator = row.cells[0].childNodes[1];
alert(chkboxIndicator);
//
if (chkboxIndicator == indicator && Checkbox.checked == false) {
table.deleteRow(i);
rowCount--;
i--;
}
}
which has an alert message of "undefined". I tried .value and .text as well and no progress. How do I get the text from the childNodes?
Since you're purportedly using jQuery, why not use it to get your checkboxes and simplify your life?
$("#mytableid tr").each(function() {
// find the checkbox (assuming you have only one; if not, use :eq(n))
var $checkbox = $("input:checkbox", this);
// if it's checked, delete this row
if ($checkbox.is(":checked"))
$(this).remove();
});
There are crazier ways to do it, but I thought that example would be illustrative. :)
Are you sure that chkboxIndicator is not null?
If it is null it will alert as undefined.
Try
for (var i = 0; i < rowCount; i++)
{
var row = table.rows[i];
// Add null check for row
if (row != null)
{
//This is where I am having trouble
// Add null check for cell
if (row.cells[0] != null)
{
var chkboxIndicator = row.cells[0].childNodes[1];
// Added null check for chkboxIndicator
if (chkboxIndicator != null)
{
alert(chkboxIndicator);
if (chkboxIndicator == indicator && Checkbox.checked == false)
{
table.deleteRow(i);
rowCount--;
i--;
}
}
else
{
alert('chkboxIndicator is NULL');
}
}
}
}
Depends on what your DOM actually looks like.
I suspect your problem is that childNodes[1] could be a text node.
You probably want to use a selector on the cell instead, such as:
$(row.cells[0]).find("input[type='checkbox']");
If you want it to be fast too, use jOrder (http://github.com/danstocker/jorder).
Create a jOrder table with your data. Make sure to have a field 'checked' in it with true or false values, and put an index on that column and the ID:
var table = jOrder(data)
.index('id', ['ID'])
.index('checked', ['checked'], { grouped: true });
Then, you can rebuild your grid with the unchecked rows removed using table.where([{ 'checked': true }]) as the source.
But first, populate your grid with the data from the jOrder table. Use table.flat() to obtain the flat table.
When you build the grid, bind the record IDs to the checkboxes e.g. by $('#elem').data(). When a checkbox gets (un)checked, update the 'checked' value in the table:
var before = table.where([{ 'ID': 5 }])[0];
var after = table.where([{ 'ID': 5 }])[0];
after.checked = true;
table.update(before, after);
Note that you shouldn't do this for each item on an "(un)check all" event though. In that case just simply pass on table.flat() or set the 'checked' values in the buffer returned by it, and then issue a table.reindex() and THEN call table.where(...).
The block of code that I was missing was the following
var row = table.rows.item(i).cells;
var cellLength = row.length;
var cellVal = row.item(5).innerHTML;
Thanks for leading me in the right direction

Categories

Resources