我正在研究C# winforms. 我有一个gridview正在显示来自 的数据database。另外,我正在将gridview数据导出到 Excel 文件。
private void BtnExport_Click(object sender, EventArgs e)
{
// To start
Cursor cursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string location = Path.Combine(dir, "Having_Dues_" + name + "_Till_" + date +"_at_" + DateTime.Now.ToString("hh_mm_ss")+ ".xlsx");
ExportToExcel(dtData, location);
// To finish
//_longOperation.Stop();
Cursor.Current = Cursors.Default;
}
public static void ExportToExcel(DataTable DataTable, string ExcelFilePath = null)
{
try
{
int ColumnsCount;
if (DataTable == null || …Run Code Online (Sandbox Code Playgroud) 我的DataGridView.DataSource有问题,它耗费了我很多时间来解决这个问题.以下是代码:
string[] queryWords = singleQuery.Split(' '); // Split the query words according the "space" character
// Compose the SQL select query
string selectClause = @"SELECT ID, CategoryID, Name, UnitID, Price, QuantityAgen, QuantityBanjer, QuantityMalalayang, QuantitySorong FROM Product WHERE ";
// Add the where clauses inside the SQL select query
for (int i = 0; i < queryWords.Length; i++)
{
selectClause += "(Name LIKE '%" + queryWords[i] + "%')";
if (i < queryWords.Length - 1)
selectClause += " AND ";
}
// …Run Code Online (Sandbox Code Playgroud) 我尝试了 ClearSelection 方法,它正在取消选择行,但所选单元格仍然突出显示
我想检查datagridview中的"many"列是否为空.
我的代码
for (int i = 0; i < (gridx.Rows.Count - 1); i++)
{
col = gridx.Rows[i].Cells["code"].Value.ToString();
col4 = gridx.Rows[i].Cells["many"].Value.ToString();
}
if (col4 == "")
{
MessageBox.Show("Many is empty");
this.Focus();
return;
}
else
{
//my code in here
}
Run Code Online (Sandbox Code Playgroud)
但它没有显示错误"很多是空的"
请帮帮我..谢谢之前
我想将数据从 DataGridView 传输到另一个,这是我的代码示例:
private void btnShow(object sender, EventArgs e)
{
DataTable dtr = new DataTable();
dtr.Columns.Add(new DataColumn("Name", typeof(string)));
dtr.Columns.Add(new DataColumn("Label", typeof(string)));
dtr.Columns.Add(new DataColumn("Domain", typeof(string)));
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataRow erow = dtr.NewRow();
erow[0] = dataGridView1.Rows[i].Cells[0].Value.ToString();
erow[1] = dataGridView1.Rows[i].Cells[1].Value.ToString();
erow[2] = dataGridView1.Rows[i].Cells[2].Value.ToString();
dtr.Rows.Add(erow);
}
dataGridView2.DataSource = dtr;
}
Run Code Online (Sandbox Code Playgroud)
我还在NullReferenceException11 号线收到。
c# ado.net exception-handling datagridview nullreferenceexception
您好我在网上找到了这个代码,它似乎做了我想要的.不过它是用c#写的,我不熟悉.任何多语言程序员都可以将其转换为vb.net?非常感谢您给予的任何帮助!
foreach (DataGridViewColumn clm in grdView.Columns)
{
Bool FoundData = false;
foreach (DataGridViewRow row in grdView.Rows)
{
if (row.Cells[clm.Index].Value.ToString() != string.Empty)
{
FoundData = true;
break;
}
}
if (!FoundData)
{
grdView.Columns[clm.Index].Visible = false;
}
}
Run Code Online (Sandbox Code Playgroud) 我在DataGridView上有一个事件将数据复制到剪贴板,但我的事件没有检测到Ctrl+ C正确按下(我必须按Ctrl+ C约15次,以使此事件检测Ctrl+ C按下).
这是代码:
private void DataGridView_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.Control | Keys.C))
{
DataObject d = DataGridView.GetClipboardContent();
Clipboard.SetDataObject(d);
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么这样?
我按Ctrl+ 时会发生什么的屏幕截图C

我曾经MS-Access填写DataGridView我的C#Windows 窗体应用程序。当我从 Access 数据库查询数据时,它显示正确。但是当我再次按下查看数据按钮时,它会添加空列。这个过程重复。我用了
dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
Run Code Online (Sandbox Code Playgroud)
清除DataGridView但没有运气。
我需要转换这个mysql表:
FirstName | LastName | Street | City | State | Zipcode | Contact Number
----------+----------+-------------+---------+-------+---------+----------------
John | Doe | 123 W. Main | Chicago | IL | 60600 | (312)555-7458
Jane | Doe | 321 W. Main | Chicago | IL | 60600 | (312)555-6628
Run Code Online (Sandbox Code Playgroud)
到datagridview中的这个表:
Name | Address | Contact Name
---------+-------------------------------+--------------
John Doe | 123 W. Main Chicago, IL 60600 | (312)555-7458
Jane Doe | 321 W. Main Chicago, IL 60600 | (312)555-6628
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的代码,但datagridview列0和1出现空白
string Query = …Run Code Online (Sandbox Code Playgroud) 我正在尝试将a绑定List<dynamic>到DataGridView DataSource属性.虽然编译时没有错误,但也没有显示任何列.
如果我预先创建列,我会显示要显示的行,但它们中没有数据.
简单地说,如何List<dynamic>在DataGridView中正确使用对象?
c# ×10
datagridview ×10
.net ×2
vb.net ×2
winforms ×2
ado.net ×1
copy-paste ×1
dapper ×1
datasource ×1
directory ×1
excel ×1
ienumerable ×1
linq ×1
mysql ×1
radgrid ×1