如果字段缺少序列号,C#窗口形式用空白列填充数据网格 [英] C# window forms fill datagrid with blank columns if field missing sequence number
问题描述
我正在尝试将数据库中的数据填充到gridview上。它可以正常填充数据,但如果缺少记录,我需要添加一个空行。我有组合框,其中列出了图像名称,如果缺少特定图像,我需要使用它进行比较我需要在之前添加的记录和数据库中的下一条记录之间添加空白记录。
对于前。我在combobox中有10张图片。我保存了6张图像的数据,然后保存了最后2张图像的数据,但错过了两张图像(7,8)。因此,当我单击按钮时,它将打开另一个带有gridview的窗口,以显示键入的数据和从数据库中提取的记录之间的空白行。
我想要获取一些实现这一点的想法。任何帮助或建议表示赞赏。
I am trying to populate data from the database onto the gridview. It works fine data is populated, but I need to add a blank row if the record is missing. I have combobox with image names listed in it and I need to use this for comparison if particular image is missing I need to add blank record in its place between previous record added and next record from database.
For ex. I have 10 images in combobox. I have saved data from 6 images and then from last 2 images, but missed two images(7,8). So when I click button it would open another window with gridview to show the data keyed in and blank rows between the records pulled from database.
I am trying to get some idea to implement this. Any help or suggestions appreciated.
SetValue = Marriage.SetValue;
cmbcount = Marriage.cmbcount;
DataRow dr;
string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_CAMR_BatchVerify";
con.Open();
cmd.Connection = con;
cmd.Parameters.AddWithValue("@BATCH_NAME", SetValue);
cmd.Parameters.AddWithValue("@ERR_CODE", "");
cmd.Parameters.AddWithValue("@ERR_MSG", "");
cmd.Parameters.AddWithValue("@TABLE_NAME", "");
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
for (int i = dt.Rows.Count; i < cmbcount; i++)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dt.AcceptChanges();
dataGridView1.DataSource = dt;
//dataGridView1.DataBind();
}
}
}
}
我尝试了什么:
我已经为剩下尚未保存的图像填充了空白记录。
What I have tried:
I havetried to populate blank records for rest of the images not saved yet.
推荐答案
单向我试图这样做并且工作正常,可能需要一些修改
One way I tried to do this and works fine, might need some modifications
SetValue = Marriage.SetValue;
cmbcount = Marriage.cmbcount;
DataRow dr;
string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[dbConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
SqlCommand comd = new SqlCommand();
comd.CommandType = CommandType.StoredProcedure;
comd.CommandText = "usp_CAMR_BatchVerify";
con.Open();
comd.Connection = con;
comd.Parameters.AddWithValue("@BATCH_NAME", SetValue);
comd.Parameters.AddWithValue("@ERR_CODE","");
comd.Parameters.AddWithValue("@ERR_MSG", "");
comd.Parameters.AddWithValue("@TABLE_NAME", "");
SqlDataAdapter sda1 = new SqlDataAdapter(comd);
DataTable dt1 = new DataTable();
dt1.Columns.Add("A");
dt1.Columns.Add("B");
dt1.Columns.Add("C");
dt1.Columns.Add("D");
dt1.Columns.Add("E");
dt1.Columns.Add("F");
dt1.Columns.Add("G");
dt1.Columns.Add("H");
dt1.Columns.Add("I");
dt1.Columns.Add("J");
dt1.Columns.Add("K");
SqlDataReader dr1 = comd.ExecuteReader();
int i1 = 1;
while (dr1.Read())
{
for (int i3 = i1; i3 <= cmbcount; i3++)
{
DataRow dr01 = dt1.NewRow();
setimg = dr1["field1"].ToString();
string tempimg = setimg.Substring(3, 4);
tmpimg = Convert.ToInt32(tempimg);
/* fields to be entered into dt1
as dr01["A"] = dr1["A"]*/
if (i1 == tmpimg)
{
//to add rows from database
dt1.Rows.Add(dr01);
i1++;
}
else
{
// to add missing rows between the rows from the database
for (int i = i1; i < tmpimg; i++)
{
dr01 = dt1.NewRow();
dt1.Rows.Add(dr01);
dt1.AcceptChanges();
dataGridView1.DataSource = dt1;
i1++;
}
}
}
dataGridView1.DataSource = dt1;
}
// to add rows at the end
for (int i = dt1.Rows.Count; i < cmbcount; i++)
{
dr = dt1.NewRow();
dt1.Rows.Add(dr);
}
dt1.AcceptChanges();
dataGridView1.DataSource = dt1;
}
这篇关于如果字段缺少序列号,C#窗口形式用空白列填充数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!