我可以在设计时为DGV设置最大和最小尺寸高度和宽度值,但不能在代码中设置.这将无法编译,因为"无法修改'System.Windows.Forms.Control.MaximumSize'的返回值,因为它不是变量":
dataGridViewPlatypi.MaximumSize.Height = dataGridViewPlatypi.Size.Height;
dataGridViewPlatypi.MinimumSize.Height = dataGridViewPlatypi.Size.Height;
dataGridViewPlatypi.MaximumSize.Width = dataGridViewPlatypi.Size.Width;
dataGridViewPlatypi.MinimumSize.Width = dataGridViewPlatypi.Size.Width;
Run Code Online (Sandbox Code Playgroud)
如果MaximumSize不是变量,为什么我可以在IDE中修改它?
使用VB.Net
我想使datagird单元格值为零而不是null
例如
Datagridview1
Id value1 value2 value3
01 null null 0
02 0 null 10
03 100 null 100
...
Run Code Online (Sandbox Code Playgroud)
预期产出
Id value1 value2 value3
01 0 0 0
02 0 0 10
03 100 0 100
...
Run Code Online (Sandbox Code Playgroud)
我有超过80列和100行,而不是逐个检查单元格任何其他方法可用
试过的代码
Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)
If e.ColumnIndex = 0 AndAlso e.Value IsNot Nothing Then
If CInt(e.Value) = 0 Then
e.Value = ""
End If
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
上面的代码不能正常工作,当我从数据库中添加datagridview中的详细信息时,也显示空单元格.
如何使零而不是null.任何其他方法或建议
需要VB.Net代码帮助
我试图控制两个DataGridView,只有一个DataGridView垂直滚动条可见.
我想在gridview中添加int值.继承我的C#代码:
foreach (GridViewRow r in GridView1.Rows)
{
int I = (Convert.ToInt32(r.Cells[5].Text)
+ Convert.ToInt32(r.Cells[6].Text)
+ Convert.ToInt32(r.Cells[7].Text)
+ Convert.ToInt32(r.Cells[8].Text)
+ Convert.ToInt32(r.Cells[9].Text));
r.Cells[10].Text = Convert.ToString(I);
}
Run Code Online (Sandbox Code Playgroud)
例外:"输入字符串的格式不正确"
它早先工作得很好.我切换到另一个系统,附加数据库,在这里和那里做了一些更改,现在它不能正常工作.加载页面时,gridview单元格已具有值.我只是添加(求和)这些值,并在单击按钮时在同一gridview的另一个单元格中显示添加.
任何帮助赞赏.
这是代码:
BindingSource bs = new BindingSource();
DataTable tbl(string sql)
{
OracleConnection con = new OracleConnection(connectionstring);
OracleDataAdapter adp = new OracleDataAdapter(sql, con);
DataSet ds = new DataSet();
adp.Fill(ds, "tbl");
return ds.Tables["tbl"];
}
void GetData()
{
bs.DataSource = Class1.tbl("select USER_ID ,EMP_NAME as pName,EMP_MOBILE from TBLUSERS");
datagridview1.Columns[0].DataPropertyName = "USER_ID";
datagridview1.Columns[1].DataPropertyName = "pName";
datagridview1.Columns[2].DataPropertyName = "EMP_MOBILE";
datagridview1.DataSource = bs;
}
void ClearAllRows()
{
datagridview1.Rows.Clear();
//The error occurs here
}
Run Code Online (Sandbox Code Playgroud)
此处发生错误如何删除DataGridView的所有行?我的DataGridView是BindingSource
我想知道哪种最有效的方法来获取DataGridView行中第一列的值。
目前,我正在使用以下代码:
List<string> SelectedRows = new List<string>();
foreach (DataGridViewRow r in dgv.SelectedRows)
{
SelectedRows.Add(r.Cells[0].Value.ToString());
}
int index = Convert.ToInt32(SelectedRows[0]);
Run Code Online (Sandbox Code Playgroud)
这很好用;但是,有没有更有效的选择呢?
虽然关于使用Control.Invoke从工作线程更新GUI控件有很多关于SO的问题/答案,但我还是无法清楚地了解从控件读取数据而不更新它的主题.
示例:我在Windows窗体上有一个DataGridView.从工作线程,我想检查DGV上特定单元格的值,但我不需要/想要更新该值.从工作线程我调用一个方法,该方法将DataGridView对象作为其唯一参数,然后该方法检查DataGridView中的特定单元格但不修改它,在这种情况下我是否需要使用.Invoke?
现在代码似乎运行正常,没有明显的警告而没有使用.Invoke.但是,我不清楚这是否安全,因为它不会更新DGV,而只是查看DGV的细胞而不修改它们中的任何一个.
有人可以对此有所了解吗?我很抱歉,如果这是一个重复的问题,但我找不到任何其他问题,只关注只读一个控件而不在多线程情况下更新/修改它.
谢谢.
从工作线程调用的示例方法:
private DataGridViewRow getRowObject(DataGridView dgv)
{
foreach (DataGridViewRow row in dgv.Rows)
{
if ((int)dgv[specialColumn, row.Index].Value == 100)
return row;
}
}
Run Code Online (Sandbox Code Playgroud) 我在从数据库中删除所选行时遇到问题,事实上,我在C#中有一个Form,其中包含连接到数据库的dataGridView和一个"Delete"按钮,当我在按钮上显示时,这应该删除所选的信息dataGridView和database中的row(cell [0]和cell [1]).现在,我在从数据库中删除所选行时遇到问题这是我的代码:
private void button4_Click(object sender, EventArgs e)
{
if (journalDataGridView.SelectedRows.Count == 1)
{
DataGridViewRow row = journalDataGridView.SelectedRows[0];
journalDataGridView.Rows.Remove(row);
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand sql = new SqlCommand("delete from journal where code_journal='" + journalDataGridView.CurrentRow.Cells[0].Value.ToString() + "'AND intitule='" + journalDataGridView.CurrentRow.Cells[1].Value.ToString() + "';", connection);
connection.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
dataGridView包含两列"code_journal and initule",感谢您的帮助
我想从一个datagridview复制到另一个datagridview。
我尝试了下面的代码,但第一列中仍然有所有数据:
For c = 0 To ReadDataDataGridView.Rows.Count - 1
For t = 0 To ReadDataDataGridView.Columns.Count - 1
DataGridView1.Rows.Add(ReadDataDataGridView.Rows(c).Cells(t).Value)
Next
Next
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
TextDataGridViewCheckBoxColumndatagridview ×10
c# ×8
winforms ×3
vb.net ×2
asp.net ×1
database ×1
datagrid ×1
delete-row ×1
scrollbar ×1
sql ×1