Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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.
Posted
Updated 30-Sep-16 4:58am
Comments
j snooze 29-Sep-16 17:23pm    
If I understand your question correctly couldn't you use
dt.Rows.Insert(int, int) first one is the position to add the row at, the second number is the number of rows you want to insert.
Member 12076824 30-Sep-16 11:00am    
Thanks for response and sorry for confusion. I was trying to insert row only if its not saved yet. It may be at the end or may be some where in the middle of some rows
Karthik_Mahalingam 29-Sep-16 23:50pm    
for (int i = dt.Rows.Count; i < cmbcount; i++)
this will add rows at end only, not in the middle(missing part)
post a screenshot if possible.
Member 12076824 30-Sep-16 11:06am    
Thanks for response, I was working on the part it worked fine as you pointed at the end. I as trying to get help with inserting blank rows in the middle, so it was not there
Karthik_Mahalingam 1-Oct-16 0:45am    
ok

1 solution

One way I tried to do this and works fine, might need some modifications

C#
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;
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900