如何避免在DataGridView中绑定Emptyrow? [英] How to Avoid Emptyrow binding in DataGridView?

查看:71
本文介绍了如何避免在DataGridView中绑定Emptyrow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 以下是我的设计师代码:

Hi The below is my designer code:

this.dgridAddProperties.AllowUserToResizeRows = false;
this.dgridAddProperties.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dgridAddProperties.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
this.dgridAddProperties.BackgroundColor = System.Drawing.Color.White;
this.dgridAddProperties.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
this.dgridAddProperties.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
this.dgridAddProperties.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgridAddProperties.Location = new System.Drawing.Point(7, 32);
this.dgridAddProperties.Name = "dgridAddProperties";
this.dgridAddProperties.RowHeadersVisible = false;
dataGridViewCellStyle1.NullValue = null;
this.dgridAddProperties.RowsDefaultCellStyle = dataGridViewCellStyle1;
this.dgridAddProperties.Size = new System.Drawing.Size(321, 170);
this.dgridAddProperties.TabIndex = 12;
this.dgridAddProperties.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgridAddProperties_CellContentDoubleClick);


当我将DataTable绑定到DataGridView时(将空记录添加到DataGridView中)
例如:DataTable1.Rows.Count = 12;
但是:DataGridView1.Rows.Count = 13;
如果您有任何想法,请告诉我.

问候,
帕湾.

[更正了某些格式]


When i am binding DataTable to DataGridView (null record is added to the DataGridView)
Ex: DataTable1.Rows.Count = 12;,
but: DataGridView1.Rows.Count = 13;
Let me know if you have any idea.

Regards,
Pawan.

[Corrected some formatting]

推荐答案

嗨..

代替使用您的代码,请尝试以下代码,该代码仅从数据库中获取数据并显示在网格视图中,即:


字符串query =从<表名>中选择*,其中< condition>" ;;
SqlConnection con =新的SqlConnection("Connection String");
SqlCommand Com =新的SqlCommand(query,con);
SqlDataAdapter da =新的SqlDataAdapter(com);
DataSet ds = new DataSet();
da.fill(ds,"Show");//您可以使用任何名称代替show
GridView1.dataSource = ds.Tables ["Show"];
GridView1.dataBind();



此代码将避免空行.
Hi..

Instead of using your code try this code which simply fetch the data from the database and display in the grid view that is:


String query="select * from <Table name> where <condition>";
SqlConnection con=new SqlConnection("Connection String");
SqlCommand Com=new SqlCommand(query,con);
SqlDataAdapter da=new SqlDataAdapter(com);
DataSet ds=new DataSet();
da.fill(ds,"Show");// You can give any name instead of show
GridView1.dataSource=ds.Tables["Show"];
GridView1.dataBind();



this Code Will avoid empty row.




还是我排空了.


下面是我的方法.........

< pre lang ="msil">公共数据集FillGridview(字符串查询)
{
System.Data.SqlClient.SqlConnection dbConn = null;
System.Data.DataSet ds =新的DataSet();
如果(ConnectionString.Length& gt; 0)
{
试试
{
dbConn =新的System.Data.SqlClient.SqlConnection(ConnectionString);
dbConn.Open();
//string Query =& quot; Select d,TradingStatus from dbo.TRADINGSTATUS& quot ;;
System.Data.SqlClient.SqlCommand dbCmd =新的System.Data.SqlClient.SqlCommand(Query,dbConn);
System.Data.SqlClient.SqlDataAdapter da =新的System.Data.SqlClient.SqlDataAdapter(dbCmd);
da.Fill(ds,&"Table0& quot;);
如果(ds.Tables.Count& gt; 0)
{
返回ds;
}
其他
返回ds;
}
捕捉{return ds; }
终于
{
if(dbConn.State.ToString().Equals(&"Open& quot;)))
{
dbConn.Close();
ds.Dispose();
dbConn.Dispose();
}
}
}
否则返回ds;
}</pre>


< pre lang ="cs">公共无效FillGridview(System.Windows.Forms.DataGridView dgv,字符串查询)
{
DataSet dsFgv = new DataSet();
dsFgv = FillGridview(Query);
dgv.DataSource = dsFgv.Tables [&"Table0& quot;] ;;
}</pre>


以上两种方法都在DataClass.cs(类文件)中

在Form1.cs中,我编写了以下代码.


DataClass dc =新的DataClass();
dc.FillGridview(dgridAddProperties,选择[名称]作为"名称,来自morepropertiesgeneration的数据类型,其中assignment =" true");
Hi

Still i am getting empty row.


The below are my methods.........

<pre lang="msil">public DataSet FillGridview(string Query)
{
System.Data.SqlClient.SqlConnection dbConn = null;
System.Data.DataSet ds = new DataSet();
if (ConnectionString.Length &gt; 0)
{
try
{
dbConn = new System.Data.SqlClient.SqlConnection(ConnectionString);
dbConn.Open();
//string Query = &quot;Select ID,TradingStatus From dbo.TRADINGSTATUS&quot;;
System.Data.SqlClient.SqlCommand dbCmd = new System.Data.SqlClient.SqlCommand(Query, dbConn);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(dbCmd);
da.Fill(ds,&quot;Table0&quot;);
if (ds.Tables.Count &gt; 0)
{
return ds;
}
else
return ds;
}
catch { return ds; }
finally
{
if (dbConn.State.ToString().Equals(&quot;Open&quot;))
{
dbConn.Close();
ds.Dispose();
dbConn.Dispose();
}
}
}
else return ds;
}</pre>


<pre lang="cs">public void FillGridview(System.Windows.Forms.DataGridView dgv, string Query)
{
DataSet dsFgv = new DataSet();
dsFgv = FillGridview(Query);
dgv.DataSource = dsFgv.Tables[&quot;Table0&quot;];
}</pre>


The above two methods are in DataClass.cs (Class file)

In Form1.cs i wrote the below code.


DataClass dc = new DataClass();
dc.FillGridview(dgridAddProperties, "select [Name] as ''Name'',DataType from morepropertiesgeneration where assignment=''true''");


这篇关于如何避免在DataGridView中绑定Emptyrow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆