i wana use gridview on my page which i select the table name on dropdownlist then gridview show that table and i wanna use delete and update buttons on gridview help me pls
Printable View
i wana use gridview on my page which i select the table name on dropdownlist then gridview show that table and i wanna use delete and update buttons on gridview help me pls
[QUOTE=sharpnet;30881]i wana use gridview on my page which i select the table name on dropdownlist then gridview show that table [/QUOTE]
For this follow steps below
1. create a function BindGrid with input param tableName as string
2. In function BindGrid create a SqlDataAdapter with sql query "Select * from "+ tableName +""
3. Create a DataTable object and fill it with SqlDataAdapater
4. Pass it as DataSource to DataGrid
5. On SelectedIndexChange of DropDownList call function BindGrid and pass DropDownList.SelectedItem as param into it
[QUOTE=sharpnet;30881]i wanna use delete and update buttons on gridview help me pls[/QUOTE]
For this u can set properties of grid to have update and delete buttons
Hope this help u. If still have some problem then pls ask.
---V V---
Vikas Vaidya
[I]Please mark this post as Thank if u found it useful[/I]
HI I AM TAKING HERE 2 text boxes(1 for id ,2 for name) and one button , if u enter any thing in those text boxes and press button then data will display in grid
<asp:datagrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 88px; POSITION: absolute; TOP: 224px"
runat="server" BackColor="MistyRose" Width="368px" AutoGenerateColumns="False" BorderColor="Blue">
<AlternatingItemStyle BackColor="#FFC0C0"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="Black" BackColor="#8080FF"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="Select" HeaderText="Select Custid" CommandName="Select"></asp:ButtonColumn>
<asp:BoundColumn DataField="id" HeaderText="Location Code">
<HeaderStyle Font-Bold="False" ForeColor="Black"></HeaderStyle>
<ItemStyle Font-Bold="False" ForeColor="Black"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="address" HeaderText="Business Location">
<HeaderStyle Font-Bold="False" ForeColor="Black"></HeaderStyle>
<ItemStyle Font-Bold="False" ForeColor="Black"></ItemStyle>
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Deleting Custid" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="Select")
{
string item1 = e.Item.Cells[1].Text;
string item2 = e.Item.Cells[2].Text;
Page.RegisterClientScriptBlock("_close","<script>window.opener.setvalue1('" + item1 +"','" +item2 + "'); window.close(); </script>");
}
if(e.CommandName=="Delete")
{
string item1 = e.Item.Cells[1].Text;
string item2 = e.Item.Cells[2].Text;
SqlConnection con=new SqlConnection("server=*******;Database=*******;uid=*******;pwd=*******");
con.Open();
SqlCommand cmd=new SqlCommand("Delete gfgf where id='"+item1+"' and address='"+item2+"'",con);
cmd.ExecuteNonQuery();
Page.RegisterStartupScript("MyScript","<script>alert('Your data has been successfully deleted')</script>");
binddatas();
}
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
binddatas();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
binddatas();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
TextBox qtyText = (TextBox)e.Item.Cells[1].Controls[0];
TextBox priceText = (TextBox)e.Item.Cells[2].Controls[0];
String id = qtyText.Text;
String address = priceText.Text;
DataSet ds=new DataSet();
SqlConnection con=new SqlConnection("server=*******;Database=*******;uid=*******;pwd=*******");
con.Open();
SqlCommand cmd=new SqlCommand("update ******* set address ='"+address+"' where id='"+id+"'",con);
cmd.ExecuteNonQuery();
DataGrid1.DataBind();
binddatas();
}
private void Button1_Click(object sender, System.EventArgs e)
{
binddatas();
}
private void binddatas()
{
string s=txtbusinesslocation.Text;
string s1=s+"%";
string p=txtlocationcode.Text;
string p1=p+"%";
if(s1!="%")
{
DataSet ds=new DataSet();
SqlConnection con=new SqlConnection("server=*******;Database=*******;uid=*******;pwd=*******");
con.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from ******* where address like'"+s1+"'",con);
da.Fill(ds);
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
else if(p1!="%")
{
DataSet ds=new DataSet();
SqlConnection con=new SqlConnection("server=*******;Database=*******;uid=*******;pwd=*******");
con.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from ******* where id like'"+p1+"'",con);
da.Fill(ds);
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
}