Text in DataTable not displayed in GridView - c#

I am creating a DataTable and populating the rows/columns in it.
However when I bind it to a GridView, I cannot see the text I assign to one column.
Code:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
DataRow dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = "some text";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 2;
dr["Column1"] = "more text";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 3;
dr["Column1"] = "and more text";
dt.Rows.Add(dr);
Gridview2.DataSource = dt;
Gridview2.DataBind();
The gridview only displays the first Column (with the numbers 1-3) but the 2nd column with the text does not display on the gridview.
Any ideas?
Thanks!
EDIT: The 1st column gridview is in BoundField whereas the 2nd column is within TemplateFied...ItemTemplate.
<asp:gridview ID="Gridview2" runat="server" ShowFooter="true" GridLines="None"
AutoGenerateColumns="false" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="#" ItemStyle-Width="30px" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="300"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd1" runat="server" Text="Add Statement" onclick="ButtonAdd1_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>

You have not used the second column in your gridview.
Either use another bound field as follows
<asp:BoundField DataField="Column1" HeaderText="#" ItemStyle-Width="30px" />
Or Modify your template filed as follows
<ItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%# Eval("Column1") %>' runat="server" Width="300">
</asp:TextBox>
</ItemTemplate>
Edit 1
Your structure of the gridview is strange as you have
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd1" runat="server" Text="Add Statement"
onclick="ButtonAdd1_Click" />
</FooterTemplate>
inside <asp:TemplateField>.

You can do like this the Markup. Use label/textbox based on your requirement
<asp:TemplateField HeaderText="Column1">
<ItemTemplate>
<asp:Label ID="lblColumn1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Column1")%'></asp:Label>
</ItemTemplate> </asp:TemplateField>

Related

Send column number value to asp MaxLength

I have a column in database where integer values are stored (4, 6, 8, and NULL).
These integer values are supposed to be max allowed length of TextBox, this is the table.
This is code from c# where I passed Characteristic names and integers(max number of places for textboxes from database dynamically) for now.
protected void ddlBC_SelectedIndexChanged(object sender, EventArgs e)
{
//ddlKar.Items.Clear();
LogicTableAdapters.getLvLOneIntegerTableAdapter getKar = new LogicTableAdapters.getLvLOneIntegerTableAdapter();
DataTable dtKar = getKar.getLvLOneInteger(ddlBC.SelectedValue);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2]{ new DataColumn("CharacteristicName", typeof(string)) new DataColumn("MaxNoPlaces", typeof(string))});
foreach (DataRow dr in dtKar.Rows)
{
dt.Rows.Add(dr["CharacteristicName"].ToString(), dr["MaxNoPlaces"].ToString());
}
gvKarakteristike.DataSource = dt;
gvKarakteristike.DataBind();
}
This is the ASPX markup:
<asp:GridView ID="gvKarakteristike" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="LevelID" OnRowDataBound="gvKarakteristike_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Characteristics">
<ItemTemplate>
<asp:Label ID="Characteristics" runat="server" Width="150px" Height="30px" Font-Names="Georgia" margin-Left="100px" Text='<%# Bind("CharacteristicName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:DropDownList ID="ddlOpis" AppendDataBoundItems="true" Width="142px" Height="35px" Font-Names="Georgia" margin-Left="100px" runat="server">
<asp:ListItem Text="" Value="" />
</asp:DropDownList>
<asp:TextBox ID="txtBoxOpis" runat="server" Font-Names="Georgia" margin-Left="100px" Text="" MaxLength='<%# Bind("MaxNoPlaces") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
I need to this dynamically directly from database because database will grow over time so will the number of textboxes.
The problem is with this part of the code:
<asp:TextBox ID="txtBoxOpis" runat="server" Font-Names="Georgia" margin-Left="100px" Text="" MaxLength='<%# Bind("MaxNoPlaces") %>'></asp:TextBox>
There's an error when I run the app there:
An exception of type 'System.InvalidCastException' occurred in App_Web_kn224wf0.dll but was not handled in user code
I think the problem is when the NULL value is send from the database to the asp, then it can convert it to MaxLength.
Can someone please help me to resolve this issue ?
Thanks in advance !
TextBox.MaxLength property expects value type of int, as in this declaration:
public virtual int MaxLength { get; set; }
Because MaxNoPlaces column declared with string data type in DataTable bound with GridView.DataBind() method, you will encounter InvalidCastException when binding it directly to the corresponding property. You should use typeof(int) and do integer conversion such like Convert.ToInt32() or int.Parse() before binding:
dt.Columns.AddRange(new DataColumn[2]{ new DataColumn("CharacteristicName", typeof(string)),
new DataColumn("MaxNoPlaces", typeof(int))});
foreach (DataRow dr in dtKar.Rows)
{
// perform integer conversion
dt.Rows.Add(dr["CharacteristicName"].ToString(), Convert.ToInt32(dr["MaxNoPlaces"]));
}
If you done the conversion before binding to textbox control, then binding conversion like <asp:TextBox runat="server" MaxLength='<%# Convert.ToInt32(Bind("MaxNoPlaces")) %>' ... /> becomes unnecessary.

Displaying GridView at Page Load

I would like to manually add rows to a GridView, and display it at Page Load. For some reason, my current code shows an empty GridView.
Default.aspx
<asp:GridView ID="AllocationGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
AutoPostBack="true">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
<Columns>
<asp:TemplateField HeaderText="Asset Class">
<ItemStyle Font-Size="13px" Width="20%" />
<ItemTemplate>
<asp:Label ID="AssetLabel" runat="server" ReadOnly="true" Text="" BorderWidth="0px"
Style="text-align: left;"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemStyle Font-Size="13px" Width="20%" />
<ItemTemplate>
<asp:TextBox ID="WeightTextBox" runat="server" ReadOnly="true" BorderWidth="0px"
Style="text-align: left;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#EBEBEB" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FirstAllocationGridViewRow();
}
}
protected void FirstAllocationGridViewRow()
{
DataTable table = new DataTable();
string[] assets = new string[] { "Cash", "US Equity", "Fixed Income", "BAS", "International" };
table.Columns.Add(new DataColumn("Col1", typeof(string)));
table.Columns.Add(new DataColumn("Col2", typeof(double)));
DataRow dr = table.NewRow();
for (int i = 0; i < assets.Count(); i++)
{
dr = table.NewRow();
dr["Col1"] = assets[i];
dr["Col2"] = DBNull.Value;
table.Rows.Add(dr);
}
ViewState["currentAllocationTable"] = table;
AllocationGridView.Visible = true;
AllocationGridView.DataSource = table;
AllocationGridView.DataBind();
}
your cs code is fine the problem is in your layout code. text property is not bind to table field name
Text='<%# Bind("Col1") %>'
here is the complete gridview
<asp:GridView ID="AllocationGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
AutoPostBack="true">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
<Columns>
<asp:TemplateField HeaderText="Asset Class">
<ItemStyle Font-Size="13px" Width="20%" />
<ItemTemplate>
<asp:Label ID="AssetLabel" runat="server" ReadOnly="true" Text='<%# Bind("Col1") %>' BorderWidth="0px"
Style="text-align: left;"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemStyle Font-Size="13px" Width="20%" />
<ItemTemplate>
<asp:TextBox ID="WeightTextBox" runat="server" Text='<%# Bind("Col2") %>' ReadOnly="true" BorderWidth="0px"
Style="text-align: left;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#EBEBEB" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Remove the AutoPostBack="true" field in Gridview control. And make sure the Data-table have correct values for the columns and please see Text="" in your gridview labels . You need give values for the labels.
See this link , It's may helps you
Your problem is with your Template Fields as you cannot set values to them using a DataTable, change them to BoundFields or set AutoGenerateColumns="True"
Tried your code with my answer, it worked fine.

Template field column got removed when we remove any column from gridview

I have a gridview control on my web form with 1 template field column.
And i am adding some bounded columns at run time from code behind.
And some times I've removed some of the previously added columns from code behind.
After removing any column gridview losses template column.
What is the cause behind this and how can i prevent template column with out setting EnableViewState="false".
Edit-1
.aspx page Code
<asp:GridView ID="grvSum" runat="server" Width="100%" GridLines="None" AllowPaging="True" ShowFooter="true"
PageSize="25" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="false" AllowSorting="true" EnableViewState="false"
OnRowUpdating="grvSum_RowUpdating" OnPageIndexChanging="grvSum_PageIndexChanging" OnSorting="grvSum_Sorting"
OnRowDataBound="grvSum_RowDataBound" Font-Size="10px">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="dtype" runat="server" CommandName="update"
CssClass="lbl" Font-Underline="true" style="cursor:pointer;" Text="Details" >
</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="20px"/>
<FooterTemplate>
<asp:Label ID="lblFooter" runat="server" Text="Total"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" HorizontalAlign="Center"/>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center" BackColor="#507CD1" Font-Bold="True" ForeColor="White" Height="30px" Wrap="true"/>
<PagerStyle HorizontalAlign="Right" CssClass="GridPager" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Center"/>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" CssClass="headerSortUp" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" CssClass="headerSortDown"/>
</asp:GridView>
Code for adding and removal of column
public void AddBoundedColumns(GridView grv, DataColumnCollection dtDataSourceColumns)
{
var gridBoundColumns = grv.Columns.OfType<BoundField>();
foreach (DataColumn col in dtDataSourceColumns)
{
//Check existence of column in gridview
if (gridBoundColumns.Any(bf => bf.DataField.Equals(col.ColumnName)) == false)
{
//Declare the bound field and allocate memory for the bound field.
BoundField bfield = new BoundField();
//Initalize the DataField value.
bfield.DataField = col.ColumnName;
//Initialize the HeaderText field value.
bfield.HeaderText = col.ColumnName;
bfield.SortExpression = col.ColumnName;
//Add the newly created bound field to the GridView.
grv.Columns.Add(bfield);
}
}
gridBoundColumns = grv.Columns.OfType<BoundField>();
int z = 0;
for (int x = 0; x < gridBoundColumns.Count(); x++)
{
BoundField c = gridBoundColumns.ElementAt(z);
if (!dtDataSourceColumns.Contains(c.HeaderText))
{
grv.Columns.Remove(c);
}
else
{
z++;
}
}
}
instead of grv.Columns.Remove(c);, try following while removing the column
grv.columns.RemoveAt(index); //"index" is the index of column you want to remove

C# - Populating Gridview

I want to create a grid view with one column containing empty textboxes where the user can input a number (quantity), some regular columns and a column dedicated to images.
I have the following code in C#:
Label_Error.Visible = false;
DataTable dt = new DataTable();
dt.Columns.Add("Quantity", typeof(TextBox));
dt.Columns.Add("Book ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Author", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Price", typeof(float));
dt.Columns.Add("Currency", typeof(string));
dt.Columns.Add("Image", typeof(string));
DataRow row1 = dt.NewRow();
row1["Quantity"] = new TextBox();
row1["Book ID"] = 1;
row1["Name"] = "Moby Dick";
row1["Author"] = "Herman Melville";
row1["Description"] = "Adventure Book";
row1["Price"] = 10;
row1["Currency"] = "EUR";
row1["Image"] = ResolveUrl("~/Images/Logo.png");
dt.Rows.Add(row1);
GridView_Products.DataSource = dt;
GridView_Products.DataBind();
This is what I am getting at the output:
As you can see, the quantity column of empty textboxes is not being shown and the image is not being shown neither. How can I solve these two problems please?
Update
This is the code in the .aspx page:
<asp:GridView ID="GridView_Products" runat="server" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
HorizontalAlign="Center">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
You need to create column by yourself instead of auto generating them.
Here is an example -
<asp:GridView ID="GridView_Products" runat="server" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
HorizontalAlign="Center" AutoGenerateColumns="False">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
<Columns>
<asp:BoundField DataField="Book ID" HeaderText="Book ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Author" HeaderText="Author" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:BoundField DataField="Currency" HeaderText="Currency" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="QantityTextBox" runat="server" Text='<%# Eval("Quantity") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Image") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
DataTable dt = new DataTable();
dt.Columns.Add("Quantity", typeof(int)); // Make sure this is integer
dt.Columns.Add("Book ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Author", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Price", typeof(float));
dt.Columns.Add("Currency", typeof(string));
dt.Columns.Add("Image", typeof(string));
DataRow row1 = dt.NewRow();
row1["Quantity"] = 1;
row1["Book ID"] = 1;
row1["Name"] = "Moby Dick";
row1["Author"] = "Herman Melville";
row1["Description"] = "Adventure Book";
row1["Price"] = 10;
row1["Currency"] = "EUR";
row1["Image"] = "~/Images/Logo.png";
dt.Rows.Add(row1);
GridView_Products.DataSource = dt;
GridView_Products.DataBind();
Here are more about GridView column types.
Not knowing the requested specs, I would suggest using an EditItemTemplate on your GridView and placing a textbox control inside that.
Do you have to programatically create your gridview? How about this...
<asp:gridview>
<columns>
<asp:TemplateField HeaderText="...">
<ItemTemplate>
<asp:Label ID="lbl_Quantity" runat="server" Text='<%# Eval("PutDateFieldNameHere") %>'
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Textbox ID="txt_Quantity" runat="server" Text='<%# Eval("DataFieldName") %>'></asp:Textbox>
</EditItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" CommandName="EditQty" Text="Edit" CommandArgument='<%# ("ItemId") %>' />
</ItemTemplate>
</columns>
</asp:gridview>
This design requires a button to show the textbox, but there are other ways to implement this. If you go with the button approach make sure you wire up a CommandName event in your code behind and that the primary key for each item quantity you are changing is bound to the button.

delete a row from the gridview from .cs

This is my aspx page
<asp:GridView ID="GridViews1" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None"
>
<Columns>
<asp:TemplateField HeaderText = "S.No">
<ItemTemplate>
<asp:Label ID="Sno" runat="server" Text= '<%#Eval("id")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Name">
<ItemTemplate >
<a href= "<%# Eval("Photo") %>" > <%# Eval("name") %> </a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Photo">
<ItemTemplate>
<img src='<%# Eval("Photo") %>' alt='<%# Eval("Name") %>' height= "50px" width = "50px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
and this is my .cs file
public partial class people_db_mysql : System.Web.UI.Page
{
String MyConString = "SERVER=localhost;" +
"DATABASE=shortandsweet;" +
"UID=root;" +
"PASSWORD=;";
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection(MyConString);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM people_details;", conn);
conn.Open();
DataTable dataTable = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dataTable);
GridViews1.DataSource = dataTable;
GridViews1.DataBind();
}}
now the output i get is
[ S.NO, Name , Photo , sno, name and photo ]
the first three fields are from the aspx page which i want it to be displayed and i dont want the other 3 fields to be displayed any ideas how i might achieve that ?
and moreover ive tried this
GridViews1.Columns[3or4or5].Visible = false; //and it says array out of bounds
however i can hide the 0,1,2 fields with the same command, is there a way to hide the rows generated through the .cs files ?
if i try to limit the rows through the select query it doesnt display anything, i just get a blank screen.
The Gridview control has a property 'AutoGenerateColumns' which has to be set to 'false', otherwise it will autogenerate the columns, even if you have manually added them.
<asp:GridView ID="GridViews1" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="false"
>

Categories

Resources