Hi Team,
I have a search button on the form which is used to search the database and then dispalys data in the webgird. And I have checkbox colum in my ultrawebgrid which is used to identify which row is selected.
Individually they works fine ,but after the seach button clicked the selected row id which idendtified by the checkbox is not right .It gives me the id from the begining of the whole the datebase, not form the searched data. Will any one help me? I will give my sample colde.
Thank in adavace.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<%@ Register assembly="Infragistics35.WebUI.UltraWebGrid.v9.1, Version=9.1.20091.1015, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" namespace="Infragistics.WebUI.UltraWebGrid" tagprefix="igtbl" %><%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:Button ID="BtnSearch" runat="server" Text="Search" onclick="BtnSearch_Click" /> <asp:Button ID="Btn_Edit" runat="server" Text="Edit" onclick="Btn_Edit_Click" /> <br /> <igtbl:UltraWebGrid ID="UltraWebGrid1" runat="server" DataSourceID="SqlDataSource1" Height="200px" Width="325px"> <bands> <igtbl:UltraGridBand> <Columns> <igtbl:TemplatedColumn> <CellTemplate> <asp:CheckBox ID="singlecheckbox" runat="server" /> </CellTemplate> </igtbl:TemplatedColumn><igtbl:UltraGridColumn Key="Id" IsBound="True" DataType="System.Int32" BaseColumnName="Id"><Header Caption="Id"></Header></igtbl:UltraGridColumn><igtbl:UltraGridColumn Key="FirstName" IsBound="True" BaseColumnName="FirstName"><Header Caption="FirstName"><RowLayoutColumnInfo OriginX="1"></RowLayoutColumnInfo></Header>
<Footer><RowLayoutColumnInfo OriginX="1"></RowLayoutColumnInfo></Footer></igtbl:UltraGridColumn><igtbl:UltraGridColumn Key="LastName" IsBound="True" BaseColumnName="LastName"><Header Caption="LastName"><RowLayoutColumnInfo OriginX="2"></RowLayoutColumnInfo></Header>
<Footer><RowLayoutColumnInfo OriginX="2"></RowLayoutColumnInfo></Footer></igtbl:UltraGridColumn><igtbl:UltraGridColumn Key="Department" IsBound="True" BaseColumnName="Department"><Header Caption="Department"><RowLayoutColumnInfo OriginX="3"></RowLayoutColumnInfo></Header>
<Footer><RowLayoutColumnInfo OriginX="3"></RowLayoutColumnInfo></Footer></igtbl:UltraGridColumn><igtbl:UltraGridColumn Key="Job" IsBound="True" BaseColumnName="Job"><Header Caption="Job"><RowLayoutColumnInfo OriginX="4"></RowLayoutColumnInfo></Header>
<Footer><RowLayoutColumnInfo OriginX="4"></RowLayoutColumnInfo></Footer></igtbl:UltraGridColumn></Columns>
<addnewrow view="NotSet" visible="NotSet"> </addnewrow> </igtbl:UltraGridBand> </bands> <displaylayout allowcolsizingdefault="Free" allowcolumnmovingdefault="OnServer" allowdeletedefault="Yes" allowsortingdefault="OnClient" allowupdatedefault="Yes" bordercollapsedefault="Separate" headerclickactiondefault="SortMulti" name="UltraWebGrid1" rowheightdefault="20px" rowselectorsdefault="No" selecttyperowdefault="Extended" stationarymargins="Header" stationarymarginsoutlookgroupby="True" tablelayout="Fixed" version="4.00" viewtype="OutlookGroupBy"> <framestyle backcolor="Window" bordercolor="InactiveCaption" borderstyle="Solid" borderwidth="1px" font-names="Microsoft Sans Serif" font-size="8.25pt" height="200px" width="325px"> </framestyle> <pager minimumpagesfordisplay="2"> <PagerStyle BackColor="LightGray" BorderStyle="Solid" BorderWidth="1px"> <borderdetails colorleft="White" colortop="White" widthleft="1px" widthtop="1px" /> </PagerStyle> </pager> <editcellstyledefault borderstyle="None" borderwidth="0px"> </editcellstyledefault> <footerstyledefault backcolor="LightGray" borderstyle="Solid" borderwidth="1px"> <borderdetails colorleft="White" colortop="White" widthleft="1px" widthtop="1px" /> </footerstyledefault> <headerstyledefault backcolor="LightGray" borderstyle="Solid" horizontalalign="Left"> <borderdetails colorleft="White" colortop="White" widthleft="1px" widthtop="1px" /> </headerstyledefault> <rowstyledefault backcolor="Window" bordercolor="Silver" borderstyle="Solid" borderwidth="1px" font-names="Microsoft Sans Serif" font-size="8.25pt"> <padding left="3px" /> <borderdetails colorleft="Window" colortop="Window" /> </rowstyledefault> <groupbyrowstyledefault backcolor="Control" bordercolor="Window"> </groupbyrowstyledefault> <groupbybox> <boxstyle backcolor="ActiveBorder" bordercolor="Window"> </boxstyle> </groupbybox> <addnewbox hidden="False"> <boxstyle backcolor="Window" bordercolor="InactiveCaption" borderstyle="Solid" borderwidth="1px"> <borderdetails colorleft="White" colortop="White" widthleft="1px" widthtop="1px" /> </boxstyle> </addnewbox> <activationobject bordercolor="" borderwidth=""> </activationobject> <filteroptionsdefault> <filterdropdownstyle backcolor="White" bordercolor="Silver" borderstyle="Solid" borderwidth="1px" customrules="overflow:auto;" font-names="Verdana,Arial,Helvetica,sans-serif" font-size="11px" height="300px" width="200px"> <padding left="2px" /> </filterdropdownstyle> <filterhighlightrowstyle backcolor="#151C55" forecolor="White"> </filterhighlightrowstyle> <filteroperanddropdownstyle backcolor="White" bordercolor="Silver" borderstyle="Solid" borderwidth="1px" customrules="overflow:auto;" font-names="Verdana,Arial,Helvetica,sans-serif" font-size="11px"> <padding left="2px" /> </filteroperanddropdownstyle> </filteroptionsdefault> </displaylayout> </igtbl:UltraWebGrid> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BDGQPTConnectionString %>" SelectCommand="SELECT * FROM [Users]"></asp:SqlDataSource> </div> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:FormView ID="FormView1" runat="server" DataKeyNames="UserId" DataSourceID="SqlDataSource2"> <EditItemTemplate> Id: <asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' /> <br /> FirstName: <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>' /> <br /> LastName: <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Bind("LastName") %>' /> <br /> Department: <asp:TextBox ID="DepartmentTextBox" runat="server" Text='<%# Bind("Department") %>' /> <br /> Job: <asp:TextBox ID="JobTextBox" runat="server" Text='<%# Bind("Job") %>' /> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" /> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" /> </EditItemTemplate> <InsertItemTemplate> FirstName: <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>' /> <br /> LastName: <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Bind("LastName") %>' /> <br /> Department: <asp:TextBox ID="DepartmentTextBox" runat="server" Text='<%# Bind("Department") %>' /> <br /> Job: <asp:TextBox ID="JobTextBox" runat="server" Text='<%# Bind("Job") %>' /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="插入" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" /> </InsertItemTemplate> <ItemTemplate> Id: <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' /> <br /> FirstName: <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("FirstName") %>' /> <br /> LastName: <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("LastName") %>' /> <br /> Department: <asp:Label ID="DepartmentLabel" runat="server" Text='<%# Bind("Department") %>' /> <br /> Job: <asp:Label ID="JobLabel" runat="server" Text='<%# Bind("Job") %>' /> <br /> </ItemTemplate> </asp:FormView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:BDGQPTConnectionString %>" SelectCommand="SELECT * FROM [Users]"></asp:SqlDataSource> </ContentTemplate>
</asp:UpdatePanel> <asp:Button ID="btnShowPopup" runat="server" style="display:none" /> <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" DynamicServicePath="" Enabled="True" TargetControlID="btnShowPopup" PopupControlID="DivPopup" > </cc1:ModalPopupExtender> </div> </form></body></html>
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;using Infragistics.WebUI.UltraWebGrid;using Infragistics;
public partial class Default4 : System.Web.UI.Page{ public string id;
protected void Page_Load(object sender, EventArgs e) {
} protected void BtnSearch_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=BDGQPT;User ID=sa;Password=123"); conn.Open(); string strSql = "Select * from [Users] where [Department]='R&D'"; DataTable DT = new DataTable(); DT = GetDataTable(strSql, "UsersData"); this.UltraWebGrid1.DataSourceID.Remove(0); this.UltraWebGrid1.DataSource = DT;
}
protected DataTable GetDataTable(string sqlstr, string tablename) { SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=BDGQPT;User ID=sa;Password=123"); conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn); DataSet ds = new DataSet(); da.Fill(ds, tablename); DataTable dt = ds.Tables[tablename]; return dt;
} protected void Btn_Edit_Click(object sender, EventArgs e) { this.FormView1.ChangeMode(FormViewMode.Edit);
Infragistics.WebUI.UltraWebGrid.TemplatedColumn TCol = (TemplatedColumn)this.UltraWebGrid1.Bands[0].Columns[0]; string t = TCol.Type.ToString(); foreach (CellItem item in TCol.CellItems) { System.Web.UI.WebControls.CheckBox chkIsHaveRight = (System.Web.UI.WebControls.CheckBox)item.FindControl("singlecheckbox"); if (chkIsHaveRight.Checked == true) { id = item.Cell.Row.Cells[1].ToString(); SqlDataSource2.SelectCommand = "SELECT * FROM [Users] WHERE [id]=" + id; break; } } this.FormView1.DataBind(); this.UpdatePanel1.Update(); this.ModalPopupExtender1.Show(); }}
Hi sunqy1212,
If you have any questions, please feel free to contact me.
I have been looking into the behavior that you have described and I could suggest you to clear the grid’s DataSourceID using the following syntax: UltraWebGrid1.DataSourceID = null, and after setting the new data source on BtnSearch_Click, call DataBind method:
DataTable dt = new DataTable();
dt = GetDataTable(strSql, "UserData");
this.UltraWebGrid1.DataSourceID = null;
this.UltraWebGrid1.DataSource = dt;
UltraWebGrid1.DataBind();
Please let me know if this helps.