在datagridview特定列中显示数据

问题描述:

大家好,

我正在使用单独的方法和处理过的proc加载datagridview.
我的问题是,这些值现在在第一个列中彼此下方填充,我需要将这些值加载到其指定的列中.

如何指定值应加载到哪一列.

EDIS_Status应该在我的datagridview的第二列中加载,kW111550在下一列中加载第三列,依此类推...

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...

 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!!!

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


试试这个代码.


try this code.




我想您的for循环中有问题.

Hi,

I guess there is a problem in your for loop.

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.

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


strcValue = "";
                    strcValue = dgvLoadProfile["cTime", 0].Value.ToString();

                    dgvLoadProfile["cEDIS_Status", 0].Value = strcValue;
                    strcValue = "";
                    dgvLoadProfile["cTime", 0].Value = strcValue;