I am having a gridview in which I don't want to display Header Text for two columns and two columns should have same header name (INFA). Gridview Looks Like:
<asp:GridView ID="GridView6" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="DayOfWeek" HeaderText="" ItemStyle-Width="30" />
<asp:BoundField DataField="DateOfMonth" HeaderText="" ItemStyle-Width="30" />
<asp:BoundField DataField="Emp_Name" HeaderText="INFA" ItemStyle-Width="30" />
<asp:BoundField DataField="Group_Name" HeaderText="INFA" ItemStyle-Width="30" />
<asp:BoundField DataField="Emp_Id" HeaderText="Mainframe" ItemStyle-Width="30" />
</Columns>
</asp:GridView>
I need to flip this gridview and make rows into columns and columns into rows. The logic was working fine when I had different names in Header Text for all DataFields. But in my requirement I don't need a Header text for two columns and two columns must have same header text (As shown above in Gridview). When I run my logic with no column name as shown above I get this error:
Exception Details: System.Data.DuplicateNameException: A column named ' ' already belongs to this DataTable.
My logic is:
protected void btnConvert_Data()
{
System.Data.DataTable dt = new System.Data.DataTable("GridView_Data");
foreach (TableCell cell in GridView6.HeaderRow.Cells)
{
if (cell.Text == "")
{
dt.Columns.Add("");
}
else
{
dt.Columns.Add(cell.Text);
}
}
dt.Rows.Add("IST Hours");
//dt.Rows.Add("8:45AM-6PM");
foreach (GridViewRow row in GridView6.Rows)
{
dt.Rows.Add();
for (int i = 0; i < row.Cells.Count; i++)
{
dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
}
}
gvColumnsAsRows.DataSource = FlipDataTable(dt);
gvColumnsAsRows.DataBind();
gvColumnsAsRows.HeaderRow.Visible = false;
}
To flip rows into columns and vice versa:
public static System.Data.DataTable FlipDataTable(System.Data.DataTable dt)
{
System.Data.DataTable table = new System.Data.DataTable();
//Get all the rows and change into columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
table.Columns.Add(Convert.ToString(i));
}
DataRow dr;
//get all the columns and make it as rows
for (int j = 0; j < dt.Columns.Count; j++)
{
dr = table.NewRow();
dr[0] = dt.Columns[j].ToString();
for (int k = 1; k <= dt.Rows.Count; k++)
dr[k] = dt.Rows[k - 1][j];
table.Rows.Add(dr);
}
return table;
}
I am getting the above mentioned error in btnConvert_Data() on line dt.Columns.Add(cell.Text);. Can anyone please help me with this issue?
My final Output when I flip the Gridview should look like this:
column header text and column name are different, which column name must be unique in a DataTable.
so for you instance, you can specify a different random name to the columns which have no column header text
and also, you need to detect if the Header is real string.Empty or something just looks like empty, in your case, the header text is never an empty value, it's
so your logic will never hit, i mean cell.Text=="" will always returns false
so here is the solution
foreach (TableCell cell in GridView6.HeaderRow.Cells)
{
if (cell.Text == " ")
{
dt.Columns.Add(Guid.NewGuid().ToString()); //just some random value, i use guid, you can use anything you like to keep it unique.
}
else
{
dt.Columns.Add(cell.Text);
}
}
You need to make sure that every column has a unique name. You can do that by checking if the column name exists and if so make it a unique one by adding the index to the column name.
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
string cellText = GridView1.HeaderRow.Cells[i].Text;
//check if the column name exists and if so make it unique
if (dt.Columns.Contains(cellText))
{
dt.Columns.Add(cellText + "_" + i);
}
else
{
dt.Columns.Add(cellText);
}
}
Related
I usually don't post questions, and I know this answer is already out there somewhere but I'm not finding it for some reason. Basically, I have a datagridview that has a variable number of columns, and variable number of rows. So when I'm building it for the screen, I first add the columns using this code:
// Set column names
for (int i = 0; i < count; i++)
{
fieldName = fromFileFields[i];
dtaRecordDisplay.Columns.Add(fromFileFields[i}, fromFileFields[i});
}
Now, I would like to populate the rows in the view with something like this:
// Set row data
for (int i = 0; i < count; i++)
{
dtaRecordDisplay.Rows.Add(Row1, Row2, etc., etc., etc..........);
}
I'm having a hard time figuring out the loop to be able to add all rows of data at once. Does anyone have any ideas on how to accomplish that?
Thanks in advance!
In this case I would fill a DataTable with columns and rows to work with, and bind that to the grid.
DataTable dt = new DataTable();
// first add your columns
for (int i = 0; i < count; i++)
{
dt.Columns.Add(fromFileFields[i]);
}
// and then add your rows
for (int i = 0; i < count; i++)
{
var row = dt.NewRow();
// Set values for columns with row[i] = xy
dt.Rows.Add(row);
}
datagridview.DataSource = dt;
You need to make a DataTable, and bind that to your grid view. Then add the rows and colums to that, like Marc says:
https://stackoverflow.com/a/35604744/5882760
The reason for that is, that the columns of the grid view instance only contain information on how to display the data from the attached DataTable.
They contain the information about what columbs to display, in what order, etc.
Try following code, you can add rows to dataGridViews without out any data Sources or data table.
dataGridView1.AllowUserToAddRows = true;
dtaGridView1 row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
row.Cells[0].Value = "Your cell 0 Data";
row.Cells[1].Value = "Your cell 1 Data";
row.Cells[2].Value = "Your cell 2 Data";
………….
row.Cells[n].Value = "Your cell n Data";
dataGridView1.Rows.Add(row);
I want to select column from data grid view.
Then, I want to add data in cells number 5 for the selected column.
Below code is not worked.
for (int i = 0; i < dgvSRP.Rows.Count; ++i)
{
dgvSRP.Rows.Add();
DataGridViewRow row = this.dgvSRP.Rows[i];
row.Cells[5].Value =txtJumlahPEsanan.Text;
}
foreach(var dgvRow in dgvSRP.Rows)
{
dgvRow.Cells[4].Value = txtJumlahPEsanan.Text; // The 5th column is index 4. Indexes always start at 0.
}
Here is my code i have select specific column value and assign it to textbox. May this solve your problem. Remember i have two cells rows thats why i kept cell[1] because index start from 0.
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i == 5)
{
textBox3.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
}
}
Footer is set to visible and I can see it's being created. However when total (table sums) are passed over to it, there's error indicating GridView2.FooterRow is null...
totTable refers to a DataTable carrying totals.
aspx:
ShowHeader="true" ShowFooter="true" FooterStyle-CssClass="FooterStyle"
cs:
DataRow dr = totTable.Rows[0];
foreach (DataControlField col in GridView2.Columns)
{
foreach (DataColumn dc in totTable.Columns)
{
int i = GridView2.Columns.IndexOf(col);
GridView2.FooterRow.Cells[i].Text = dr[i].ToString();
}
}
What's the root cause behind this?
use following code after binding gridview:
public void CountGrandTotal()
{
int sum = 0;
for (int i = 0; i <grdproduct.Rows.Count ; i++)
{
Label lblprice = (Label)grdproduct.Rows[i].FindControl("Label5");
sum += int.Parse(lblprice.Text);
}
Label lblgtotal = (Label)grdproduct.FooterRow.FindControl("Label7");
lblgtotal.Text = sum.ToString();
Add the footer on the OnLoad method of the GridView.
<asp:GridView ID="Gv"
runat="server"
ShowFooter="true"
OnLoad="Gv_Load">
I have an issue when displaying bit fields in a GridView, when the GridView is rendered the bit field is blank.
Code to bind to my GridView:
protected void InitGridViewDisplay(GridView mygrid, Button mybutton, DataTable mydt, int i)
{
mygrid.DataSource = mydt;
mygrid.DataBind();
mygrid.Visible = true;
}
All other fields display correctly, but, the bit fields show as empty columns?
...after further discussion...
for (int j = 0; j < mydt.Columns.Count; j++)
{
string fieldtype = Convert.ToString(mydt.Columns[j].DataType);
if (fieldtype == "System.Boolean")
{
foreach (DataRow row in mydt.Rows)
{
string getrowvalue = Convert.ToString(row[j]);
switch (getrowvalue)
{
case "False":
{
row[j] = 0;
break;
}
case "True":
{
row[j] = 1;
break;
}
}
}
}
}
...Am I missing something? Columns is still blank when displayed in the GridView...
In my project I have a GridView with checkboxs. It checks and unchecks using a "Bit" value.
<Columns>
<ItemTemplate>
<asp:CheckBox id="chkStatus" Enabled='<%# Enable(Eval("Status")) %>' runat="server" />
</ItemTemplate>
At code behind
Protected Function Enable(ByVal sStatus As Boolean) As String
If UCase(Trim(sStatus)) = True Then
Return "true"
ElseIf UCase(Trim(sStatus)) = False Then
Return "false"
End If
End Function
Arun
I have a GridView:
On Button_Click
I want the first column of my GridView with the items of ListBox1 and 2nd column with the EDIT link and from 3rd column on wards I want the headers as the items of ListBox2.
So far I am able to achieve EDIT link in the 2nd column and from 3rd column on wards the headers of the GridView with ListBox2 items.
Now I want ListBox1 items as the first column of my GridView. I have attached an image with show what exactly I want and what I have achieved so far.
Kindly help me on. Thank you.
The .cs code I have designed is:
DataTable dt = new DataTable();
DataRow rw = default(DataRow);
for (int i = 0; i < ListBox3.Items.Count; i++)
{ dt.Columns.Add(ListBox3.Items[i].ToString(),System.Type.GetType("System.String"));
}
for (int j = 0; j < count; j++)
{
rw = dt.NewRow();
for (int i = 0; i < ListBox3.Items.Count; i++)
{
rw[ListBox3.Items[i].ToString()] = " ";
}
dt.Rows.Add(rw);
}
GridView2.DataSource = dt;
GridView2.DataBind();
The asp code for GridView is :
<Columns>
<asp:TemplateField HeaderText="Locations">
<ItemTemplate>
<asp:Label ID="Lab1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
First create a event of your gridview_RowDataBound
and change value of your label
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl1= ((Label)e.Row.FindControl("Lab1"));
lbl1.text = ListBox1.items[e.Row.RowIndex].ToString();
}
}