I want to hide empty row in one particular column. I tried to but negative. Below is my code:
protected void gvDb_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow rw in gvDb.Rows)
{
if ((string.IsNullOrEmpty(rw.Cells[1].Text) | (rw.Cells[1].Text == "")))
{
rw.Visible = false;
}
}
}
for (int i = 0; i < gvDb.RowCount - 1; i++)
{
var row = gvDb.Rows[i];
if (string.IsNullOrEmpty(Convert.ToString(row.Cells[1].Value)))
{
row.Visible = false;
}
}
This will work,
use for instead of foreach to iterate all the rows except last row which is empty.
Related
enter image description hereI have 2 different datagrid, the first DG1 is my item list and DG2 is the item queues for purchased items. My goal is whenever i click an item in DG2, DG1 is also selected with the same name or id. I want to ignore the index because my item queue is different from item list order.
private void dgItems_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowindex = dgItems.Rows[e.RowIndex].Index;
int columnindex = dgItems.Columns[e.ColumnIndex].Index;
dgItemList.Rows[rowindex].Cells[columnindex].Selected = true;
}
You need to access Value property from both of datagridviews cell like
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowindex = dataGridView1.Rows[e.RowIndex].Index;
int columnindex = dataGridView1.Columns[e.ColumnIndex].Index;
foreach (DataGridViewRow row in dataGridView2.Rows)
row.Selected = false;
var cellValue1 = dataGridView1.Rows[rowindex].Cells[columnindex].Value;
foreach (DataGridViewRow row in dataGridView2.Rows)
{
var cellValue2 = row.Cells[columnindex].Value;
if (cellValue1 == cellValue2)
row.Selected = true;
else
row.Selected = false;
}
}
dataGridView1 is your dgItems and dataGridView2 is your dgItemList.
Edit:
If your both of datagridviews column are on different index then you have to provide column index to match value.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowindex = dataGridView1.Rows[e.RowIndex].Index;
int columnindex = dataGridView1.Columns[1].Index;
foreach (DataGridViewRow row in dataGridView2.Rows)
row.Selected = false;
var cellValue1 = dataGridView1.Rows[rowindex].Cells[1].Value; // 1 <= index of Name column in dgItems
foreach (DataGridViewRow row in dataGridView2.Rows)
{
var cellValue2 = row.Cells[0].Value; // 0 <= index of Name column in dgItemList
if (cellValue1 == cellValue2)
row.Selected = true;
else
row.Selected = false;
}
}
Output:
The easiest way to locate the items by name is to loop through the items in the second list, and return the index of the name that matches your query.
private int FindRowIndex(DataGridView view, string searchValue)
{
foreach(DataGridViewRow row in view)
{
if(row.Cells[1].Value.ToString().Equals(searchValue))
return row.Index;
}
return -1;
}
You then simply use this to control which item is being selected in the second table.
private void dgItems_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowindex = dgItems.Rows[e.RowIndex].Index;
int columnindex = dgItems.Columns[e.ColumnIndex].Index;
dgItemList.Rows[rowindex].Cells[columnindex].Selected = true;
string searchName = dgItemList.Rows[rowindex].Cells.Value.ToString();
int secondRowIndex = FindRowIndex(dgItemList2, searchName);
dgItemList2.Rows[secondRowindex].Cells[columnindex].Selected = true;
}
You can repeat the basic loop to locate an alternative column index if needed. Having a method to refer to gives you the ability to repeat the process if you want to click on an item in list 2, and have it select list 1.
Can please any one help me on this. I have developed a c# windows application which has DataGridView first column has checkboxes. if I click on first column header it selects all the row level check boxes except the first row. For selecting all row level check boxes I have an event of dataGridView1_ColumnHeaderMouseClick and the code is:
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
if (e.ColumnIndex == 0)
{
if (chek == 0)
{
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
string paymentValue = dataGridView1.Rows[i].Cells[18].Value.ToString();
string incmngp = dataGridView1.Rows[i].Cells[20].Value.ToString();
if (paymentValue == "N" && incmngp =="")
{
dataGridView1.Rows[i].Cells[0].Value = 1;
chek = 1;
}
}
if (chek == 1)
{
btn_update.Text = "Update";
}
}
catch (Exception ) { }
}
else if(chek==1)
{
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
dataGridView1.Rows[i].Cells[0].Value = 0;
chek = 0;
}
if (chek == 0)
{
btn_update.Text = "OK";
}
}
catch (Exception) { }
}
}
Note: chek is the variable declared on initialize stage
Set your Selection mode property of data grid view to ColumnHeaderSelect
and make sure all your 'Text' columns have SortMode set to NotSortable
UPDATE 2
In which case, Undo everything I ever told before and do that like this
Before you are assigning any DataTable to dataGridView1.
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
dataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
foreach(DataGridViewColumn dc in dataGridView1.Columns)
{
dc.SortMode = DataGridViewColumnSortMode.NotSortable;
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
UPDATE 3
Add an event handler for your dataGridView1's ColumnHeaderMouseClick Event
like below
Add the below code (Generic code if you want to use the same functionality for any column of check boxes)
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//Enter your own column index here
if(e.ColumnIndex == 0)
foreach(DataGridViewRow row in dataGridView1.Rows)
foreach (DataGridViewCell cell in row.Cells)
{
//Check if the cell type is of a CheckBoxCell
if (cell.GetType() == typeof(DataGridViewCheckBoxCell))
{
DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)cell;
c.TrueValue = "T";
c.FalseValue = "F";
if (c.Value == c.FalseValue|| c.Value == null )
c.Value = c.TrueValue;
else
c.Value = c.FalseValue;
}
}
dataGridView1.RefreshEdit();
}
This is a very bizarre bug in Winforms. The problem more generally applies not to the first row, but to the first selected cell in any row of DataGridViewCheckBoxCell(s). You can select the CheckBox cell by clicking on the check box, or select the cell outside the check box, the behavior is the same. If you select 3 check boxes in the middle of your grid, the first of those three will freeze and not update properly. If you try to clear the selection in code, with a dataGridView1.ClearSelection() method call, it still does not work.
The correct answer is to call datagridview1.RefreshEdit() right after you change the checkbox data. You can't just call it after all changes are made. It must be made for each change in the CheckBox value.
foreach (DataGridViewRow row in Results.Rows)
{
var ck = (DataGridViewCheckBoxCell) row.Cells["check"];
ck.Value = ck.TrueValue;
Results.RefreshEdit();
}
Solution, for at least a specific cell: GridView1.Rows[i].Cells[j].Text;
I've build a simple CSV-Fileupload. After the user uploaded the file he should be able to evaluate the data. When the fileupload was successful the data gets loaded into the GridView1, with this code: (Problem below the code)
string[] readCSV = File.ReadAllLines(lblFilePath.Text);
DataTable dt = new DataTable();
bool bSplitMe = false;
foreach (var rLine in readCSV)
{
if (bSplitMe)
{
string[] aSplittedLine = rLine.Split(";".ToCharArray());
try
{
dt.Rows.Add(aSplittedLine);
}
catch(System.Exception)
{
txtBoxFileOut.Text = rLine;
break;
}
}
else
{
if (rLine.ToLower().StartsWith("definedtestid;"))
{
bSplitMe = true;
string[] aSplittedLine = rLine.Split(";".ToCharArray());
foreach (var rCol in aSplittedLine)
{
dt.Columns.Add(rCol);
}
}
else
{
txtBoxFileOut.Text += rLine.ToString() + "\n";
}
}
}
dt.Columns.Remove("Column1");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "0";
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
After this the user should be able to select a row and display the data from that row in a chart.
Problem: I'm not able to read data from the cells I want, or to read from a "hardcoded" cell.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
GridViewRow row = GridView1.SelectedRow;
txtOutputfield.Text = row.Cells[2].Text;
}
Please check your cell index. Is it correct? For example: the third column will have index "2" not "3"
And, if you use a control to store the data, you need to find that control:
txtOutputfield.Text =
row.Cells[2].FindControl('placeyourcontrolnamehere').Text;
For a specific Cell this worked fine
txtOutputfield.Text = GridView1.Rows[i].Cells[j].Text;
I use the following code to find a row in DataGridView and highlight the row.
private void btnSearch_Click(object sender, EventArgs e)
{
currentMode = ModeSelection.Search;
if (cmbSearchBy.SelectedIndex == Convert.ToInt16(SearchBy.MaterialID))
{
dgvSearchResults.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
int rowIndex = -1;
try
{
foreach (DataGridViewRow row in dgvSearchResults.Rows)
{
if (row.Cells[1].Value.ToString().Equals(materialLocation.MaterialID))
{
//Select the row here
rowIndex = row.Index;
dgvSearchResults.Rows[rowIndex].Selected = true;
break;
}
}
}
catch (Exception ex) { throw ex; }
}
It works perfectly. Problem is my DataGridView has over 500 records and if the selected row is near the bottom of the DataGridView, users have to scroll all the way down to the bottom. Which code can I use to jump to the row that I am looking for? Any help will be very much appreciated!
I found out that I can use DataGridView.FirstDisplayedScrollingRowIndex Property to scroll to the selected row index and display it as the first row in DataGridView.
This is how I used in my program-
if (row.Cells[1].Value.ToString().Equals(materialLocation.MaterialID))
{
rowIndex = row.Index;
dgvSearchResults.ClearSelection();
dgvSearchResults.Rows[rowIndex].Selected = true;
dgvSearchResults.FirstDisplayedScrollingRowIndex = rowIndex;
dgvSearchResults.Focus();
break;
}
int rowIndex = -1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value.ToString().Equals(searchString))
{
rowIndex = row.Index;
break;
}
}
if (rowIndex >= 0)
{
dataGridView1.CurrentCell = dataGridView1[visibleColumnIndex, rowIndex];
}
visibleColumnIndex - selected cell must be visible
I'm trying to access the value of a cell in my GridView. I want to access the value by the cell's name, rather than by the index. How can I do this?
I do not want to access the cell by its index because there is a chance that it will change positions at any time. I know that Cells[0] would give me the value of the first index, but how about if I want to do something like Cells["NameOfCell"]?
Note: I cannot use the GridView events because all the existing code is doing it in a function called Bind() and they have something like this
public void Bind()
{
foreach (GridViewRow row in GridView1.Rows)
{
//need to access the specific value here by name
//I know this is wrong but you get the idea
string test = row.Cells["NameOfCell"].ToString();
}
}
just 4 fun:
private int nameCellIndex = -1;
private const string CellName = "Name";
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int cellIndex = 0; cellIndex < e.Row.Cells.Count; cellIndex++)
{
if (e.Row.Cells[cellIndex].Text == CellName)
{
nameCellIndex = cellIndex;
break;
}
}
}
else if (nameCellIndex != -1 && e.Row.RowType == DataControlRowType.DataRow)
{
string test = e.Row.Cells[nameCellIndex].Text;
}
}
the same, not using RowDataBound:
private int nameCellIndex = -1;
private const string CellName = "Name";
void Button1_Click(object sender, EventArgs e)
{
for (int cellIndex = 0; cellIndex < GridView1.HeaderRow.Cells.Count; cellIndex++)
{
if (GridView1.HeaderRow.Cells[cellIndex].Text == CellName)
{
nameCellIndex = cellIndex;
break;
}
}
if (nameCellIndex != -1)
{
foreach (var row in GridView1.Rows.OfType<GridViewRow>().Where(row => row.RowType == DataControlRowType.DataRow))
{
string test = row.Cells[nameCellIndex].Text;
}
}
}
If possible, get the data from your data source - the GridView should be used for displaying data and not retrieving it. It's bound to your data source, so you should be well-equipped to read from the data source too.
You can get the value from the GridView Row DataItem
See here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem.aspx