
How i get data from Datagrid with out using child commands?
I have datagrid and i want get datas from that grid. but grid has no child events, no child controls. then how can i get it?
May be the following code might help you
NOTE:(dgStateMaster is datagrid name,lblMessage is a Label to display error message)
(dt1 is datatable,ut is an object to connect the class used for opening & closign the datasource)
On ‘EditCommand’ of datagrid write the following code :
/******************************************On Edit Command*******************************************/
private void dgStateMaster_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
ViewState["i"]=this.dgStateMaster.DataKeys[e.Item.ItemIndex];
this.chkActive.Visible=true;
if(e.CommandName==”Edit”)
/*CommandName==”Edit” means that put a Button Col in datagrid and name it as Edit(u also can use other name)*/
{
Display();
}
else
{
this.lblMessage.Text=”some problem”;
this.lblMessage.ForeColor=Color.Red;
}
}
catch(Exception ex)
{
this.lblMessage.Text=ex.Message.ToString();
this.lblMessage.ForeColor=Color.Red;
this.ut.CloseConnection();
}
}
/*********************************End Edit Command*************************************/
/***********************************Display Function***********************************/
public void Display()
{
try
{
string dstr=”SELECT StateName,Active FROM StateMaster WHERE StateID”+
“=”+ViewState["i"];
this.chkActive.Visible=true;//is a checkbox
dt1=this.ut.GetDataTable(dstr);
this.txtStateName.Text=dt1.Rows[0]["StateName"].ToString();
this.chkActive.Checked=(bool)dt1.Rows[0]["Active"];
}
catch(Exception ex)
{
this.lblMessage.Text=ex.Message;
this.lblMessage.ForeColor=Color.Red;
this.ut.CloseConnection();
}
}
/************************************End Display Function*********************************/