我试过了 -
DataGridView1.DataSource=Nothing
Run Code Online (Sandbox Code Playgroud)
和
DataGridView1.DataSource=Nothing
DataGridView1.Refresh()
Run Code Online (Sandbox Code Playgroud)
和
DataGridView1.RefreshEdit()
Run Code Online (Sandbox Code Playgroud)
它们都不起作用..
我编写了一个方法,在执行时设置DataGridView的DataSource.但每次执行它时,它都会使用新值复制数据并将其附加到DGV的先前内容中.我想清除内容然后添加值..这可能吗?
我是C#的新手并对使用"var"有疑问
当我使用以下代码时,一切都很好
foreach(DataGridViewRow row in myGrid.Rows)
{
if (row.Cells[2].Value.ToString().Contains("51000"))
{
row.Cells[0].Value = "X";
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我改变DataGridViewRow为var我得到和错误的状态
'object'不包含'Cells'的定义,也没有扩展方法'Cells'可以找到接受'object'类型的第一个参数(你是否缺少using指令或汇编引用?)
在Windows窗体中,我试图DataGridView通过插入DataGridViewRows来手动填充,所以我的代码如下所示:
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvArticles);
row.Cells[0].Value = product.Id;
row.Cells[1].Value = product.Description;
.
.
.
dgvArticles.Rows.Add(row);
Run Code Online (Sandbox Code Playgroud)
但是,我想按列名添加Cell值,而不是通过索引来添加它,如下所示:
row.Cells["code"].Value = product.Id;
row.Cells["description"].Value = product.Description;
Run Code Online (Sandbox Code Playgroud)
但这样做会抛出一个错误,说它无法找到名为"code"的列.我正在设计器中设置DataGridView列,如下所示:

难道我做错了什么?我怎样才能完成我想做的事情?
我在C#3.5的WinForm应用程序中有一个DataGridView。
AllowUserToAddNewRow属性设置为true。当用户在DataGridView中键入任何文本时,另一新行将自动添加到DataGridView。我不希望添加新行,直到对当前行执行了一些检查并且所有必要的信息都已填满为止。
示例:我有一个带有空白行的DataGridView:

当我开始输入时,添加新行还为时过早:

我想要的是仅在用户输入数量后才添加新行:

我在TabPage上有一个DataGridView.当用户点击一行时,会出现第二个DGV.每行与其自己的DGV相关联,其中包含数据.我想要的是当用户从一行转到另一行时,DataGridView也会改变.到目前为止,我已经尝试过SelectionChanged事件,但是如果用户点击同一行中的单独单元格,我不希望重新加载DGV.任何帮助将不胜感激.
void dgv1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = dgv1.Rows[e.RowIndex].Index;
if (dgv1 == null)
return;
if (dgv1.Rows[e.RowIndex].Cells[rowIndex].Selected == true);
{
dgv2.Size = new Size(dgv2.Width + 800, dgv2.Height);
dgv2.Location = new Point(0, 500);
tp.Controls.Add(dgv2);
Console.WriteLine("Row clicked");
}
}
Run Code Online (Sandbox Code Playgroud)
-Tatiana
在Windows应用程序(Visual Studio)(VB)中,如何将单个行拖放到另一个帖子以允许用户重新排序该行?我还没有找到任何有价值的例子.
vb.net drag-and-drop datagridview visual-studio-2010 datagridviewrow
我有一个DataGridView根据组号分组的行。为了在视觉上分隔不同组的行,我设置了DividerHeight某个组中最后一行的 。
我想为行分隔符和行本身的鼠标事件实现不同的行为。DataGridView.HitTestInfo似乎没有办法检查这一点。有什么办法让我知道行分隔符是否被单击或者是否有任何东西掉落在其上?
我的网格外观的图像。(深灰色区域是行分隔线):
我有一些问题,现在不要如何保存一个滚动位置DataGridView.
我有超过1000行,滚动回编辑行是痛苦的.如何在刷新数据后保留滚动位置并滚动到已编辑的行?
我正在尝试使用最大记录 ID 值获取 DataGridViewRow。我能够获得记录 ID 本身,但我需要找到该行本身。
这是我获取最大记录 ID 的代码,它工作正常:
var addedRow =
dataGridView.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Cells[Glossary.RowType].Value.ToString().Equals(Glossary.RowTypeCustom))
.Select(r => Convert.ToInt(r.Cells[Glossary.RecordId].Value))
.Max();
Run Code Online (Sandbox Code Playgroud)
我试过:
.Max(r => r.Cells[Glossary.RecordId].Value);
Run Code Online (Sandbox Code Playgroud)
我不清楚如何获取行本身,所以我可以关注它:
dataGridView.CurrentCell = addedRow;
Run Code Online (Sandbox Code Playgroud) 我的系统时钟是9/16/2014(星期二)
但在代码中,我总是跳到星期一.
DayOfWeek dow = new DateTime().DayOfWeek;
int columnNumber = 0;
columnNumber = columnNumber + 0;
foreach ( DataGridViewRow row in dataGridView1.Rows )
{
switch ( dow )
{
case DayOfWeek.Monday:
columnNumber = 4;
if ( (bool) row.Cells[4].Value == true ) // crashing here with NullReferenceException
{
row.Cells["activeTodayDataGridViewCheckBoxColumn"].Value = true;
}
break;
Run Code Online (Sandbox Code Playgroud)
我有一个 DataGridView
TextDataGridViewCheckBoxColumndatagridviewrow ×10
datagridview ×9
c# ×8
winforms ×5
.net ×2
vb.net ×2
clear ×1
datasource ×1
linq ×1
mouseevent ×1
var ×1