I have a gridview that binds its rows from an objects method:
public DataSet GetObjects()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
var source = from p in CommentsList
select new { p.CommentID, p.Img, p.Name, p.Comment };
dt.Columns.Add("CommentsID");
dt.Columns.Add("Img");
dt.Columns.Add("Name");
dt.Columns.Add("Comment");
foreach (var item in source)
{
DataRow userDetailsRow=dt.NewRow();
userDetailsRow["Img"] = item.Img;
userDetailsRow["Name"] = item.Name;
DataRow commentsID = dt.NewRow();
userDetailsRow["CommentsID"] = item.CommentID;
DataRow comments = dt.NewRow();
userDetailsRow["Comment"] = item.Comment;
dt.Rows.Add(userDetailsRow);
//dt.Rows.Add(comments);
}
ds.Tables.Add(dt);
return ds;
}
My delete method. Should receive the commentsID to delete it from the database and then rebind the Gridview.. But how? do i pass the CommentsID from the gridview to this method
public void RemoveComment(int CommentsID)
{
}
GridViews Two templates:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblMessage" runat="server" Text='<%# Bind("Comment") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imgName" runat="server" imageUrl='<%# Bind("Img") %>'></asp:Image><br />
<asp:Hyperlink ID="hyperLink" runat="server" Text='<%# Bind("Name") %>' ></asp:Hyperlink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Now, I want to delete a row, how do I do that? I know that DataKeyNames exist..but how do I set the commentsID as my dataKeyNames, and how do i delete a record?
You can specify the Delete method to the objectDataSource and it will be very simple...e.g.
<asp:ObjectDataSource ID="ods" runat="server" DeleteMethod="RemoveComment" SelectMethod="GetCityByStateID" TypeName="">
<DeleteParameters>
<asp:Parameter Name="CommentsID" Type="Int32" />
</DeleteParameters>
<SelectParameters>
</SelectParameters>
</asp:ObjectDataSource>
Whenever the Gridview Delete button is hit this will automatically fire this method and delete the record.
Related
I've two columns Customer Type and File Frequency in the gridview. When the grid is in normal mode I show the values using label. On editing the row, those two columns become dropdowns. I bind the dropdowns using OnRowDataBound="RowDataBound". But, Only first dropdown(whichever first written in the method) in the RowDataBound method getting bind on the edit mode.
.aspx
<asp:GridView ID="gvManageCustomers" DataKeyNames="Ship_To" runat="server" AutoGenerateColumns="False"
OnRowEditing="EditCustomer" OnRowDataBound="RowDataBound" OnRowUpdating="UpdateCustomer"
OnRowCancelingEdit="CancelEdit" CssClass="table table-bordered table-condensed">
<Columns>
<asp:TemplateField HeaderText="Customer Type">
<ItemTemplate>
<asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>' Visible="false">
</asp:Label>
<asp:DropDownList ID="ddlgvCustomerType" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Frequency">
<ItemTemplate>
<asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddlgvFileFreq" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
.cs
public DataTable FetchCustomerType()
{
string sql = "select distinct Customer_TypeID,Customer_Type from tbl_CustomerType";
SqlDataAdapter da = new SqlDataAdapter(sql, constr);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable FetchFileFrequency()
{
string sql = "SELECT distinct FileFrequency_ID,FileFrequency FROM [tbl_FileFrequency]";
SqlDataAdapter da = new SqlDataAdapter(sql, constr);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlgvFileFreq = (DropDownList)e.Row.FindControl("ddlgvFileFreq"); //getting binded
ddlgvFileFreq.DataSource = FetchFileFrequency();
ddlgvFileFreq.DataTextField = "FileFrequency";
ddlgvFileFreq.DataValueField = "FileFrequency_ID";
ddlgvFileFreq.DataBind();
ddlgvFileFreq.Items.FindByValue((e.Row.FindControl("lblFileFreq") as Label).Text).Selected = true;
DropDownList ddlgvCustomerType = (DropDownList)e.Row.FindControl("ddlgvCustomerType");
ddlgvCustomerType.DataSource = FetchCustomerType();
ddlgvCustomerType.DataTextField = "Customer_Type";
ddlgvCustomerType.DataValueField = "Customer_TypeID";
ddlgvCustomerType.DataBind();
ddlgvCustomerType.Items.FindByValue((e.Row.FindControl("lblCustType") as Label).Text).Selected = true;
}
}
}
catch (Exception ex)
{
//log error
errorlog.WriteErrorLog(ex.ToString());
}
}
The problem is in this line, it trows an exeption. That's why only the topmost binding would work.
ddlgvFileFreq.Items.FindByValue((e.Row.FindControl("lblFileFreq") as Label).Text).Selected = true;
But it looks like you want the DropDownList to have the correct SelectedValue. This is how you can do that.
DataRowView row = e.Row.DataItem as DataRowView;
DropDownList ddlgvFileFreq = (DropDownList)e.Row.FindControl("ddlgvFileFreq");
ddlgvFileFreq.DataSource = FetchFileFrequency();
ddlgvFileFreq.DataTextField = "FileFrequency";
ddlgvFileFreq.DataValueField = "FileFrequency_ID";
ddlgvFileFreq.DataBind();
try
{
ddlgvFileFreq.SelectedValue = row["FileFrequency_ID"].ToString();
}
catch
{
}
DropDownList ddlgvCustomerType = (DropDownList)e.Row.FindControl("ddlgvCustomerType");
ddlgvCustomerType.DataSource = FetchCustomerType();
ddlgvCustomerType.DataTextField = "Customer_Type";
ddlgvCustomerType.DataValueField = "Customer_TypeID";
ddlgvCustomerType.DataBind();
try
{
ddlgvCustomerType.SelectedValue = row["Customer_TypeID"].ToString();
}
catch
{
}
<asp:TemplateField HeaderText="Customer Type">
<ItemTemplate>
<asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>'></asp:Label>
<asp:Label ID="lblCustTypeID" runat="server" Visible="false" Text='<%# (Eval("Customer_TypeID")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlgvCustomerType" runat="server" DataSource="<%# FetchCustomerType() %>" DataValueField="Customer_TypeID" DataTextField="Customer_Type" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Frequency">
<ItemTemplate>
<asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>'></asp:Label>
<asp:Label ID="lblFileFreqID" runat="server" Visible="false" Text='<%# (Eval("FileFrequency_ID")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlgvFileFreq" runat="server" DataSource="<%# FetchFileFrequency() %>" DataValueField="FileFrequency_ID" DataTextField="FileFrequency" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
In onRowEditing Event
((DropDownList)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("ddlgvCustomerType")).SelectedValue = ((Label)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("lblCustTypeID")).Text;
((DropDownList)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("ddlgvFileFreq")).SelectedValue = ((Label)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("lblFileFreqID")).Text;
This question already exists:
how to show data in gridview from arraylist in asp.net?
Closed 8 years ago.
I have a database where there is userid, problemname and status column. I am retrieving this data from database in an ArrayList and returning it. Now to show in GridView I have taken a DataTable and in the DataTable I have put three columns and I just want to show my data that is saved in the ArrayList in these columns by making one row.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
ArrayList myArrayList = ConvertDataSetToArrayList();
// Display each item of ArrayList
DataTable dt = new DataTable();
dt.Columns.Add("User Id");
dt.Columns.Add("Problem Name");
dt.Columns.Add("Status");
foreach (Object row in myArrayList)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["User Id"] = ((DataRow)row)["userid"].ToString();
dt.Rows[dt.Rows.Count - 1]["Problem Name"] = ((DataRow)row) ["problemname"].ToString();
dt.Rows[dt.Rows.Count - 1]["Status"] = ((DataRow)row)["status"].ToString();
}
GridView1.DataSource =dt;
GridView1.DataBind();
}
public ArrayList ConvertDataSetToArrayList()
{
string con = " ";
con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection objsqlconn = new SqlConnection(con);
objsqlconn.Open();
SqlCommand cmd = new SqlCommand("SELECT userid,problemname,status FROM problemtable", objsqlconn);
cmd.ExecuteNonQuery();
cmd.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = cmd;
DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet);
ArrayList myArrayList = new ArrayList();
foreach (DataRow dtRow in myDataSet.Tables[0].Rows)
{
myArrayList.Add(dtRow);
}
objsqlconn.Close();
return myArrayList;
}
Here is my html:
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="cdd">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
Why is my data is not showing in my GridView?
You need to bind your data to specific controls in your gridview. For example, you need to have labels in your gridview itemtemplate to bind your data to. Here is an example:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="cdd">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Id">
<ItemTemplate>
<asp:Label ID="lbl_userid" runat="server" Text='<%# Eval("User Id") %>' CssClass="lbl"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Problem Name">
<ItemTemplate>
<asp:Label ID="lbl_problemname" runat="server" Text='<%# Eval("Problem Name") %>' CssClass="lbl"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="status">
<ItemTemplate>
<asp:Label ID="lbl_status" runat="server" Text='<%# Eval("Status") %>' CssClass="lbl"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I agree that an ArrayList is a poor choice, but, regardless of how you're binding the data, you need to tell the gridview what it's supposed to show. You can do this using inline tags or by using an onitemdatabound trigger. I recommend you look up some more examples of gridviews.
I am working in ASP.Net with C# as code behind. The editor is Visual Studio 2012.
I created a GridView with 5 columns: the first one contains checkboxes, the next 3 have text boxes and the last one is a buttonfield, as follows (I have made AutoGenerateColumns as false because, otherwise DataBind adds more columns":
<asp:GridView ID="gvCDS" runat="server" AutoGenerateColumns="False" OnRowDeleting="gvCDS_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSelectedCDS" runat="server" Enabled="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Module Code">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSModuleCode" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Module Name">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSModuleName" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit Units">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSCreditUnits" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Clear Module" />
</Columns>
I need to show the GridView on selecting an item from a dropdown. The GridView always needs to have 8 rows. I have written the following code to show the GridView:
DataTable dt = ViewState["dt"] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("", typeof(Boolean));
dt.Columns.Add("Module Code", typeof(String));
dt.Columns.Add("Module Name", typeof(String));
dt.Columns.Add("Credit Units", typeof(String));
for (int i = 0; i < 8; i++)
{
dt.Rows.Add(dt.NewRow());
}
}
gvCDS.DataSource = dt;
gvCDS.DataBind();
ViewState["dt"] = dt;
gvCDS.Visible = true;
The user makes some inputs in the GridView and then clicks a button. In the button's click event procedure, I need to delete the checked rows from the GridView and add rows so that total number of rows remains as 8. If I simply call .DeleteRow method, the row is not getting deleted. I still see the GridView as earlier in the browser. So I first copy the content of GridView into DataTable as follows:
DataTable dt = ViewState["dt"] as DataTable;
int rowCounter = 0;
foreach (GridViewRow row in gvCDS.Rows)
{
DataRow dr = dt.Rows[rowCounter];
dr[0] = ((CheckBox)row.FindControl("cbSelectedCDS")).Checked;
dr[1] = ((TextBox)row.FindControl("tbxInputCDSModuleCode")).Text;
dr[2] = ((TextBox)row.FindControl("tbxInputCDSModuleName")).Text;
dr[3] = ((TextBox)row.FindControl("tbxInputCDSCreditUnits")).Text;
rowCounter++;
}
After that, instead of deleting rows from the GridView, I delete from the DataTable as follows:
rowCounter = 0;
int rowsDeleted = 0;
foreach (GridViewRow row in gvCDS.Rows)
{
if (((CheckBox)row.FindControl("cbSelectedCDS")).Checked)
{
dt.Rows.RemoveAt(rowCounter);
rowsDeleted++;
}
else
{
rowCounter++;
}
}
rowsDeleted now contains the number of rows that were deleted. I add that many rows in the DataTable so that the total number of rows remains as 8.
for (i = 0; i < rowsDeleted; i++)
{
dt.Rows.Add(dt.NewRow());
}
After that I bind GridView to DataTable as follows:
ViewState["dt"] = dt;
gvCDS.DataSource = dt;
gvCDS.DataBind();
The GridView still has 8 rows. That is correct. But the problem is that the entire GridView has become empty. When testing, I filled some of the rows and checked only one of them. So, the content of the unchecked rows should be there. But the entire GridView is empty. I do not know why.
Not only that, I even tested by keeping all rows unchecked, which means no row will be deleted. Still, the GridView becomes all empty.
How do I solve this problem?
Thank you.
Issue is your Text Boxes inside GridView are not bound to any field. Bind their Texts to the fields like Text='<%# Eval("Field")%>'.
Change your GridView markup to this:
<asp:GridView ID="gvCDS" runat="server" AutoGenerateColumns="False" OnRowDeleting="gvCDS_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSelectedCDS" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Module Code">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSModuleCode" Text='<%# Eval("Module Code")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Module Name">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSModuleName" Text='<%# Eval("Module Name")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit Units">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSCreditUnits" Text='<%# Eval("Credit Units")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Clear Module"/>
</Columns>
</asp:GridView>
I have GridView:
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server" AppendDataBoundItems="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="procesVerbal">
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now I want to fill this Grid with data from database, I have one method that returns datatable with all my data
when I fill mygrid:
MyGridView.DataSource = dataTable;
MyGridView.Databind();
in this case all TextBoxes and DropDownList are null.
In the code below I'm trying to create a function that receives DataTable from database and populates gridview with data. How do I make a foreach statement for dataTable.Rows that will assign text values to TextBox elements and selectedIndex value to DropDownList?
protected bool FillGridWithData(DataTable dataTable)
{bool result;
try
{
foreach (GridViewRow row in MyGridView1.Rows)
{
if (row.RowType != DataControlRowType.DataRow) break;
var ddl = (DropDownList)row.FindControl("dl_situation");
if (ddl != null)
{
ddl.DataSource = PublicStatic.Situation;
ddl.DataTextField = PublicStatic.Name;
ddl.DataValueField = PublicStatic.Code;
ddl.DataBind();
ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));
ddl.SelectedIndex = //data from datatable;
}
var tb1 = (TextBox)row.FindControl("tbNr");
if (tb1 != null)
tb1.Text =//data from datatable;
var tb2 = (TextBox)row.FindControl("tbDate");
if (tb2 != null)
tb2.Text = //data from datatable;
}
result = true;
}
catch (Exception exception)
{
result = false;
}
return result;
}
The DataField attribute in the bound field must equal to the column name in the datatable.
For binding textboxes in the template field you can use the Eval or Bind Method.
Example:
<asp:TemplateField HeaderText="procesVerbal">
<ItemTemplate>
<asp:TextBox ID="tbNr" Text='<%# Eval("ColumnName") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
and for binding the dropdown in the gridview, you can use a separate sql datasource. and specify dropdownlist DataSourceID,DataTextField and DataValueField property.
Dropdownlist Example:
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddl" runat="server" DataSourceID="SqlDataSource1" DataTextField="ProductName" DataValueField="ProductID" ></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [ProductName], [ProductID] FROM [Alphabetical list of products]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
and if you want to bind the datasource from code behind then you can use RowDataBound event of gridview
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
TextBox t = (TextBox)e.Row.FindControl("ControlID");
t.Text = "Some Text";
}
}
UPDATE: THIS IS THE WORKING VERSION.
public DataSet GetObjects()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
var source = from p in CommentsList
select new { p.Img, p.Name, p.Comment };
dt.Columns.Add("Img");
dt.Columns.Add("Name");
dt.Columns.Add("Comment");
foreach (var item in source)
{
DataRow userDetailsRow=dt.NewRow();
userDetailsRow["Img"] = item.Img;
userDetailsRow["Name"] = item.Name;
DataRow comments = dt.NewRow();
userDetailsRow["Comment"] = item.Comment;
dt.Rows.Add(userDetailsRow);
//dt.Rows.Add(comments);
}
ds.Tables.Add(dt);
return ds;
}
My GridView columns section looks like this:
<Columns>
<asp:TemplateField HeaderText="#">
<HeaderStyle Width="500px" />
<ItemStyle Width="500px" Height="100px" />
<ItemTemplate>
<asp:Label ID="lblMessage" runat="server" Text='<%# Bind("Comment") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="#">
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" Height="100px" />
<ItemTemplate>
<asp:Image ID="imgName" runat="server" imageUrl='<%# Bind("Img") %>'></asp:Image><br />
<asp:Hyperlink ID="hyperLink" runat="server" Text='<%# Bind("Name") %>' ></asp:Hyperlink>
</ItemTemplate>
</asp:TemplateField>
UPDATE: The problem that I have got now is with the size of rows..that are huge and dont update with the content within,,,example: Header 33%, Row 33%, footer 33%..even though the content of the header is 10% of the gridview..how do i fix that?
You do not have a Img property. Plain and simple. This is why you have problems accessing it: it does not exist.You should create the property AND populate it.
Update
You should do this:
var source = from p in CommentsList
select new { p.Img, p.Name, p.Comment };
dt.Columns.Add("User");
dt.Columns.Add("Comment");
dt.Columns.Add("Img");
and then this:
DataRow userDetailsRow=dt.NewRow();
userDetailsRow["Img"] = item.Img;
userDetailsRow["User"] = item.Name;
Fix where it applies.
You need to add a Column in your DataTable with the name of Img before using this in DataBinding
userDetailsRow["User"] = item.Img;
should be
userDetailsRow["img"] = item.Img;