Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am loading a datagridview using separate methods and stroed procs.
My problem is that the values is now populating underneath each other in the first colum, I need the values to load in their specified columns.

How would I be able to specifiy in which column the values should load.

EDIS_Status should load in the 2nd column of my datagridview, kW111550 in the next column the 3rd column etc...

C#
 private void LoadGrid()
        {
            try
            {
                clsConnection data = new clsConnection();
                SqlCommand sqlcomm = new SqlCommand();
                DataTable dt = new DataTable();

                sqlcomm.Connection = data.connection;
                sqlcomm.CommandType = CommandType.StoredProcedure;
                sqlcomm.CommandText = "usp_LoadProfileGrid2";
                sqlcomm.Parameters.Add("@MeterID", SqlDbType.Int).Value = iMeterID;

                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = sqlcomm;
                da.Fill(dt);
                dgvLoadProfile.ReadOnly = true;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dgvLoadProfile.Rows.Add(dt.Rows[i]["EDIS_Status"].ToString());
                }           

                clsConnection data2 = new clsConnection();
                SqlCommand sqlcomm2 = new SqlCommand();
                DataTable dt2 = new DataTable();

                sqlcomm2.Connection = data2.connection;
                sqlcomm2.CommandType = CommandType.StoredProcedure;
                sqlcomm2.CommandText = "usp_LoadProfileGrid3";
                sqlcomm2.Parameters.Add("@MeterID", SqlDbType.Int).Value = iMeterID;

                SqlDataAdapter da2 = new SqlDataAdapter();
                da2.SelectCommand = sqlcomm2;
                da2.Fill(dt2);
                dgvLoadProfile.ReadOnly = true;

                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    dgvLoadProfile.Rows.Add(dt2.Rows[i]["kW111550"].ToString());
                }
catch (Exception excException)
            {
                MessageBox.Show(excException.Message, excException.Source, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


Thanks any help would be appriciated!!!
Posted
Updated 17-Jan-12 22:19pm
v6
Comments
Anuja Pawar Indore 18-Jan-12 2:22am    
Added pre tag
Ben Paxton 18-Jan-12 2:29am    
What do you mean
Anuja Pawar Indore 18-Jan-12 2:36am    
Mean your code part is placed in pre tag using improve question. So that it becomes clear to the users to answer.

XML
<asp:TemplateField HeaderText="ProgrammeID" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblProgID" runat="server" Text='<%# Bind("ProgrammeID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

try this code.
 
Share this answer
 
Comments
Ben Paxton 18-Jan-12 2:15am    
Sorry but its not web, win forms application
Hi,

I guess there is a problem in your for loop.

C#
for (int i = 0; i < dt.Rows.Count; i++)
               {
                   dgvLoadProfile.Rows.Add(dt.Rows[i]["EDIS_Status"].ToString());
               }



Here, you are looping through the datatable rows but not looping through the grid.

check if this works.

C#
for (int i = 0; i < dt.Rows.Count; i++)
               {
                   dgvLoadProfile.Rows[i].Add(dt.Rows[i]["EDIS_Status"].ToString());
               }
 
Share this answer
 
Comments
Ben Paxton 18-Jan-12 2:49am    
No, not working. I just want to specifiy in which column the data should populate into
Ben Paxton 18-Jan-12 2:49am    
Something like..dgvLoadProfile[1, 0].Rows.Add(dt.Rows[i]["EDIS_Status"].ToString());
manognya kota 18-Jan-12 2:54am    
can you try the below code ,it works in asp..

dgvLoadProfile.Rows[0].Cells[1].Text=dt.Rows[i]["EDIS_Status"].ToString();
manognya kota 18-Jan-12 2:57am    
Dint get why you are adding a cell to gridview row, when you want to assign the data of the grid view cell .
C#
strcValue = "";
                    strcValue = dgvLoadProfile["cTime", 0].Value.ToString();

                    dgvLoadProfile["cEDIS_Status", 0].Value = strcValue;
                    strcValue = "";
                    dgvLoadProfile["cTime", 0].Value = strcValue;
 
Share this answer
 
try this
dgvLoadProfile.Columns.Add("EDIS_Status", "EDIS_Status");
dgvLoadProfile.Columns.Add("kW111550", "kW111550");
DataGridViewRow row;
            
for (int i = 0; i < dt.Rows.Count; i++)
  {
     row = new DataGridViewRow();
     row.Cells["EDIS_Status"].Value = dt.Rows[i]["EDIS_Status"].ToString();
     dgvLoadProfile.Rows.Add(row);
  }

for (int i = 0; i < dt.Rows.Count; i++)
  {
     row = new DataGridViewRow();
     row.Cells["kW111550"].Value = dt.Rows[i]["kW111550"].ToString();
     dgvLoadProfile.Rows.Add(row);
  }
 
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