0

I am generating a gridview in a horizondal way. Code works fine but a dummy row insert with name of column1 and column2. How can I prevent this insertion?? My gridview code is

str = "select MachID,EmpCode from EmpDetails where EmpCode='" + empcode + "'";
DataTable dt = GetData(str);
DataTable dt2 = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{
    dt.Columns.Add();
}
for (int i = 0; i < dt.Columns.Count; i++)
{
    dt2.Rows.Add();
    dt2.Rows[i][0] = dt.Columns[i].ColumnName;
}
for (int i = 0; i < dt.Columns.Count; i++)
{
    for (int j = 0; j < dt.Rows.Count; j++)
    {
        dt2.Rows[i][j + 1] = dt.Rows[j][i];

    } 
}
gvOrders.DataSource = dt2;
gvOrders.DataBind();

My present o/p is

Column1    Column2
MachID     101
EmpCode    ABC

I want to prevent this column1 and column2 row. How can I do this??

My GetData function is

private static DataTable GetData(string query)
{
    DataTable dt = new DataTable();
    SqlCommand cmd = new SqlCommand(query);
    String constr =        ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlDataAdapter sda = new SqlDataAdapter();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    sda.SelectCommand = cmd;
    sda.Fill(dt);
    return dt;
}
Simon Karlsson
  • 4,090
  • 22
  • 39
Semil Sebastian
  • 111
  • 1
  • 5
  • 18
  • 1
    On a side note your sql is prone to sql injection read this to avoid the sql injection prone queries http://stackoverflow.com/questions/6547986/how-to-prevent-a-sql-injection-escaping-strings – Mahesh Jan 15 '15 at 10:27
  • @ Coder of Code, Ya, its just a testing model thatsy I avoid parameterized query. – Semil Sebastian Jan 15 '15 at 10:29
  • can you post your aspx code – Mahesh Jan 15 '15 at 10:30
  • 1
    you assign `dt2.Rows[i][0] = dt.Columns[i].ColumnName;` so you store your columnName in dt2 and it will show up afterwards. - in case i misunderstood please clarify your problem! – Pilgerstorfer Franz Jan 15 '15 at 10:32
  • @Coder of Code, just a gridview controller – Semil Sebastian Jan 15 '15 at 10:33
  • @Pilgerstorfer Franz, I have posted my output sample..There you can see column1 and column2. I do not want this. I think its my logic mistake but I could not find that mistake. Its my problem – Semil Sebastian Jan 15 '15 at 10:37
  • Why are you adding `columns` for each `rows` in your `dt`? It is because of the first loop where you are adding `columns` for each `ROWS` in your `dt`. One more thing, try to add `BreakPoints` and `Debug` your code to see the problem. – DhavalR Jan 15 '15 at 11:35

2 Answers2

1

In your code-behind try to set the row header visibility to false as follows:

gvOrders.RowHeadersVisible = false;

Else in your aspx-file you can add ShowHeader="False" to your asp:GridView tag.

Thomas
  • 1,563
  • 3
  • 17
  • 37
  • I couldnt find "RowHeadersVisible" – Semil Sebastian Jan 15 '15 at 10:41
  • http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridtablestyle.rowheadersvisible%28v=vs.110%29.aspx – Thomas Jan 15 '15 at 10:43
  • The thing 'Column1' and 'Column2' is printed when the two your for loops iterates for the first time, so just put a condition to add nothing to your rows when the first execution of respective for loops is done. - Laymen solution (easy to understand and implement) – Parth Jan 15 '15 at 10:55
0

Code for Deleting duplicate rows in a data table dt

for (int i = 0; i < dt.Rows.Count-1; i++)
        {
            for (int j = i+1; j < dt.Rows.Count; j++)
            {
                if (dt.Rows[i].ItemArray.SequenceEqual(dt.Rows[j].ItemArray))
                    dt.Rows[j].Delete();
            }
        }

add these lines of code in between second and third line

Shamseer K
  • 4,964
  • 2
  • 25
  • 43